Sockets server design advice

Posted by Rob on Stack Overflow See other posts from Stack Overflow or by Rob
Published on 2010-03-30T15:20:05Z Indexed on 2010/03/30 15:23 UTC
Read the original article Hit count: 478

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about sockets