0

I have an asynchronous socket project and I need to know whenever a client was disconnected so I can remove it from the list of the connected clients.

Is there any way of checking this asynchronously?

In my mind, I could make a thread where I'm looping through all clients and check if !Client.Connected then I'll close the socket but maybe there's another way of doing this.

Leo
  • 53
  • 1
  • 8
  • What socket object are you using? What are the needs of your project? - A different class might suit your needs better e.g. TCPListener etc and what have you tried thus far? – Clint Apr 04 '13 at 20:44
  • TCP socket,I need to know when a client was disconnected. What have I tried thus far?Nothing,I've just wrote my idea in this question and I don't think it would be a good one since there could be lets say 1.000 clients..and looping through 1000 clients may take time.. – Leo Apr 04 '13 at 20:46
  • @Looping through 1000 clients would indeed take time, but ideally you should be dealing with each of these clients asynchronously, you could just check to ensure they're still accessible when you try to interact with them. – Clint Apr 04 '13 at 20:50
  • This might help you: http://stackoverflow.com/questions/2555630/how-to-tell-when-a-socket-has-been-disconnected – Clint Apr 04 '13 at 20:50
  • As I said before,in the client code I'll make a thread and loop through all the clients and check if its connected or not. it seems that this is the only way . – Leo Apr 04 '13 at 20:55
  • I mean each client should get their own thread (for 1000 clients this isn't recommended at all, you might want to investigate using tasks (System.Threading.Task), having an additional thread checking all the clients isn't going to be workable especially with large numbers of clients. – Clint Apr 04 '13 at 20:56
  • So,you're saying that I need to make a thread for each client when its connected right? – Leo Apr 04 '13 at 21:00
  • essentially yes, each client should be handled asynchronously using methods like `BeginAccept` and `BeginReceive`, but spinning up 1000 threads will kill your app and OS performance, this is why the `BeginAccept` methods are a good pattern to get into the habit of using. – Clint Apr 04 '13 at 21:02
  • Every client on a thread of its own is a lot of overhead when you have 1000 clients in mind - thats 1GB of RAM sitting unused regardless if a client is actively working + whatever CPU overhead you get. Producer/consumer threaded pattern seems better fit – Sten Petrov Apr 04 '13 at 21:18

1 Answers1

0

The following question may provide you with some guidance on how best to approach your problem and how to treat sockets with multiple clients in general

Asynchronous server socket multiple clients

Community
  • 1
  • 1
Clint
  • 6,133
  • 2
  • 27
  • 48
  • I'm using begin read,accept write etc.. but if I'm not interacting with the client and he's disconnected , the socket is still in my list and I don't want that. – Leo Apr 04 '13 at 21:13
  • @Leo then this may be of use http://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket it would appear there's no way of being notified but you will have to come up with a polling strategy somehow. – Clint Apr 04 '13 at 21:15
  • I made a single thread where I'm looping through all the sockets and check with the pool function if the socket is connected or not. – Leo Apr 04 '13 at 22:04
  • @Leo that should do the trick, but be aware that polling each socket takes an amount of time and looping over each socket could also take considerable time so you may want to restrict the number of clients you allow to be connected simultaneously. – Clint Apr 04 '13 at 22:07
  • Ehm..50% cpu usage xD for that checking – Leo Apr 04 '13 at 22:11
  • Yeah, I'd recommend in your while loop putting a Thread.Sleep of 15 milliseconds or so for each time around the loop, that will bring that right down. – Clint Apr 04 '13 at 22:12