Sockets 服务器设计建议
我们正在用 C# 编写一个套接字服务器,需要一些设计建议。
背景:客户端(来自移动设备)连接到我们的服务器应用程序,我们将它们的套接字保持打开状态,以便我们可以在需要时将数据发送回给它们。数据量各不相同,但我们通常每隔几秒就从每个客户端发送/接收数据,因此数据量相当大。同时连接的数量范围可以是 50-500(将来会更多)。
我们已经使用异步套接字编写了一个服务器应用程序并且它可以工作,但是我们遇到了一些绊脚石,我们需要确保我们正在做的事情是正确的。
我们有一个保存客户端状态的集合(目前我们没有套接字/连接池,应该吗?)。
每次客户端连接时,我们都会创建一个套接字,然后等待它们向我们发送一些数据,并在 receiveCallBack 中将它们的 clientstate 对象添加到我们的连接字典中(一旦我们验证了它们是谁)。
当客户端对象注销时,我们关闭它们的套接字,然后关闭它,并将它们从我们的客户端字典集合中删除。
想必一切都按正确的顺序发生,一切都按预期进行。
然而,几乎每天它都会停止接受连接,或者我们认为,要么它连接,要么它连接但实际上没有做任何事情,我们无法弄清楚为什么它只是停止。
有几件事我们不确定
1) 我们是否应该创建某种类型的连接池,而不仅仅是客户端套接字的字典
2) 连接但没有添加到我们的套接字中的套接字会发生什么字典,它们只是在内存中徘徊,什么都不做,我们是否应该创建另一个字典来在创建套接字后立即保存它们?
3) 确定客户端是否不再连接的最佳方法是什么?我们已经阅读了很多方法,但我们不确定最好使用哪种方法,发送数据或读取数据,如果是的话,该怎么办?
4)如果我们循环遍历连接字典来检查已释放的客户端,我们是否应该锁定字典,如果是的话,这会如何影响尝试同时使用它的其他客户端对象,它会抛出错误还是只是等待?
5) 我们经常在 ReceiveCallBack 方法中随机得到 DisposeSocketException,这是否意味着我们可以安全地从集合中删除该套接字?
我们似乎找不到任何生产类型的示例来显示这些工作。
任何建议都会受到极大的欢迎
We are writing a socket server in c# and need some advice on the design.
Background: Clients (from mobile devices) connect to our server app and we leave their socket open so we can send data back down to them whenever we need to. The amount of data varies but we generally send/receive data from each client every few seconds, so it's quite intensive. The amount of simultaneous connections can range from 50-500 (and more in the future).
We have already written a server app using async sockets and it works, however we've come across some stumbling blocks and we need to make sure that what we're doing is correct.
We have a collection which holds our client states (we have no socket/connection pool at the moment, should we?).
Each time a client connects we create a socket and then wait for them to send us some data and in receiveCallBack we add their clientstate object to our connections dictionary (once we have verified who they are).
When a client object then signs off we shutdown their socket and then close it as well as remove them from our collection of clients dictionary.
Presumably everything happens in the right order, everything works as expected.
However, almost everyday it stops accepting connections, or so we think, either that or it connects but doesn't actually do anything past that and we can't work out why it's just stopping.
There are few things that we'r'e unsure about
1) Should we be creating some kind of connection pool as opposed to just a dictionary of client sockets
2) What happens to the sockets that connect but then don't get added to our dictionary, they just linger around in memory doing nothing, should we create ANOTHER dictionary that holds the sockets as soon as they are created?
3) What's the best way of finding if clients are no longer connected? We've read some many methods but we're not sure of the best one to use, send data or read data, if so how?
4) If we loop through the connections dictonary to check for disposed clients, should we be locking the dictionary, if so how does this affect other clients objects trying to use it at the same time, will it throw an error or just wait?
5) We often get disposedSocketException within ReceiveCallBack method at random times, does this mean we are safe to remove that socket from the collection?
We can't seem to find any production type examples which show any of this working.
Any advice would be greatly received
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
4)是的,锁定它。其他线程只需要等待。
一般来说,您必须在每次访问(包括读取)时锁定字典。否则,结构可能会损坏,这可能是导致问题的原因之一。
4) Yes, lock it. The other threads will just have to wait.
In general, you have to lock the dictionary with each access, including reads. Otherwise, the structure can become corrupted, which is one possible cause for your problems.
Daryn Kiely 撰写了一篇有关 .NET 中套接字编程的好文章我认为这回答了你的几个问题。它提供了一些很好的背景知识以及客户端和服务器实现的示例。
There is a good article on socket programming in .NET by Daryn Kiely that I think answers several of your questions. It gives some good background along with examples of client and server implementation.