0

My intended-to-be-enterprise-class async TCP server will be serving 20,000+ connections at any given moment in time. I've written some code below to detect ungraceful client disconnects, but I'm not certain it will work. Is there a better way to do this?

    public void PollUsers()
    {
        while (true)
        {
            Thread.Sleep(2000);
            lock(users_lock)
            {
                foreach(Socket user_socket in users)
                {
                    if (!IsConnected(user_socket))
                        Console.WriteLine("user disconnected");
                }
            }
        }
    }

    public bool IsConnected(Socket socket)
    {
        try
        {
            return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
        }
        catch (SocketException) { return false; }
    }
  • 1
    Why do you think you need to write this code? As and when sockets are detected to no longer be connected, they'll produce exceptions *anyway* and you should gracefully shutdown those connections. – Damien_The_Unbeliever May 10 '17 at 18:25
  • This post here : http://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket explains that there are no events when a client disconnects. You're saying that instead, I can use the exception to detect disconnects, when the server tries to send information through a closed socket? – Harrison Walters May 10 '17 at 18:34
  • 1
    No amount of *pre-checking* can determine a future state when you're dealing with networks, connections, servers, etc. By the time you have your answer (e.g. the client is still connected) and *before you have an opportunity to act on it*, reality may have changed. The only way to know if a remote endpoint can receive (and respond to) some data is to **send the data**. – Damien_The_Unbeliever May 10 '17 at 18:41
  • @Damien_The_Unbeliever what if I need to deliver important message to all clients as fast as possible, but messages themselves are relatively rare. Then by detecting and disconnecting stale clients when I have no messages to send I might gain some benefit (by not doing that when time is important, that is when I actually need to deliver a message). – Evk May 10 '17 at 18:55
  • 1
    If you're an expert a building these things, then build them how you will atop TCP, UDP, etc. If you're not an expert, then *don't try to build these things yourself*. Work at higher levels. It sounds like you want *messaging* - that's not part of TCP. Pick a higher level protocol that fits your thinking. – Damien_The_Unbeliever May 10 '17 at 19:16
  • The code you propose won't work at all. With TCP, you have to have _some_ network operation for the network stack to detect the disconnect. Generally speaking, it is poor practice to attempt to detect disconnects before they matter, because TCP will recover from benign disconnects. Code that attempts to detect disconnects earlier will actually behave in a _less_ reliable manner. See marked duplicates for additional discussion. – Peter Duniho May 10 '17 at 20:53
  • Also read https://stackoverflow.com/questions/1162950/getting-started-with-socket-programming-in-c-sharp-best-practices and [The Winsock Programmer's FAQ](http://tangentsoft.net/wskfaq/). The latter isn't about .NET per se, but still contains a great deal of must-have information relevant for any network programming, including when using .NET – Peter Duniho May 10 '17 at 20:53

0 Answers0