By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

C# Client/Server Disconnection

P: 7
I have a fairly simple client/server structure using System.Net.Sockets, where the server only sends data to a collection of clients, and the clients only receive data, so, the data flow is unidirectional. My server sends the same data to each of the connected clients, at the same time through a for loop. The loop sends the data, or removes the client if it has disconnected. My problem is when the client disconnects, it can cause the server program to pause.

This is where the server distributes the data:
Expand|Select|Wrap|Line Numbers
  1.             for (int i = Clients.Count - 1; i >= 0; i--)
  2.             {
  3.                 if (((Client)Clients[i]).isAlive() && serve)
  4.                 {
  5.                     ((Client)Clients[i]).send(data.get_bytes());
  6.                 }
  7.                 else
  8.                 {
  9.                     ((Client)Clients[i]).Stop();  // releases resources associated w/ client
  10.                     Clients.RemoveAt(i);
  11.                 }
Also in the server code, there is a client handling class, and this is the send function used above:
Expand|Select|Wrap|Line Numbers
  2.                 try
  3.                 {
  4.                     ns.Write(sendB, 0, sendB.Length);
  5.                 }
  6.                 catch (***Exception) { } //if there's an error i will say that the client has disconnected, and isAlive = false;
I would like to keep it simple and have the client disconnect, whether or not the client disconnected on purpose, or the connection is cut. The actual client code is simple, and just sits in a loop waiting to call the NetworkStream read() call. I'm sure there's a better and more efficient way of handling the client disconnecting, b/c i don't want this to affect the remaining clients, if one disconnects for any reason. Any ideas? Thanks.
Nov 6 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 7,872
I had the same problem for a LOOOOOONG time, until I started using this sneaky little trick:
Expand|Select|Wrap|Line Numbers
  1. private bool CheckIsConnected(Socket s)
  2. {
  3.    bool part1 = s.Poll(1000, SelectMode.SelectRead);
  4.    bool part2 = (s.Available == 0);
  5.    if (part1 & part2)
  6.    {//connection is closed
  7.       return false;
  8.    }
  9.    return true;
  10. }
If you look at what Socket.Poll() does you can see that there are situations in which you KNOW the socket is disconnected.
(I should really have a third bool in there to deal with if there is pending connection requests, but whatever)

I actually use that in my "waiting to read" loop and if it shows up as disconnected I break out of the loop and stuff
Nov 6 '07 #2

P: 7 are you using that on the client side? i could use that on both sides, i suppose, but i'm less concerned w/ the client and what happens to it when it's disconnected. thanks for the tip.
Nov 6 '07 #3

Expert 5K+
P: 7,872
Actually I use it on the server side (I wrote a webserver, so the client is a web browser)
Nov 6 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.