C# Client/Server Disconnection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlgeris
    New Member
    • Feb 2007
    • 7

    #1

    C# Client/Server Disconnection

    I have a fairly simple client/server structure using System.Net.Sock ets, 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:
    Code:
                for (int i = Clients.Count - 1; i >= 0; i--)
                {
                    if (((Client)Clients[i]).isAlive() && serve)
                    {
                        ((Client)Clients[i]).send(data.get_bytes());
                    }
                    else
                    {
                        ((Client)Clients[i]).Stop();  // releases resources associated w/ client
                        Clients.RemoveAt(i);
                    }
    Also in the server code, there is a client handling class, and this is the send function used above:
    Code:
                    try
                    {
                        ns.Write(sendB, 0, sendB.Length);
                    }
                    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.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I had the same problem for a LOOOOOONG time, until I started using this sneaky little trick:
    Code:
    private bool CheckIsConnected(Socket s)
    {
       bool part1 = s.Poll(1000, SelectMode.SelectRead);
       bool part2 = (s.Available == 0);
       if (part1 & part2)
       {//connection is closed
          return false;
       }
       return true;
    }
    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

    Comment

    • jlgeris
      New Member
      • Feb 2007
      • 7

      #3
      interesting...n ow 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.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Actually I use it on the server side (I wrote a webserver, so the client is a web browser)

        Comment

        Working...