2

I am working on TCP multithread server with C# window application form, and I am trying to detect if the machine of client is shutdown and disconnects from the server. I have read some posts and have some ideas:

How to determine if the tcp is connected or not?

Instantly detect client disconnection from server socket

But I'm not sure where to call the function IsConnected

My code is like the following:

public BindingList<Tablet> tabletList = new BindingList<Tablet>();
private Socket socket_Server = null;
    private Thread myThread = null;
    private Socket socket_Connect = null;
    private Dictionary<string, Socket> dic = new Dictionary<string, Socket> { };
    private string RemoteEndPoint;

socket_Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPAddress ServerIP = IPAddress.Parse("192.168.2.146");
        IPEndPoint point = new IPEndPoint(ServerIP, portNum);
        socket_Server.Bind(point);
        socket_Server.Listen(50);
        myThread = new Thread(Listen_Disp);
        myThread.IsBackground = true;
        myThread.Start();
        Console.WriteLine("Server start");

private void Listen_Disp()
    {
        try
        {
            while (true)
            {

                //This is not working
                for (int i = 0; i < tabletList.Count; i++)
                {
                    if (!SocketConnected(dic[tabletList[i].ip]))
                    {
                        Console.WriteLine(RemoteEndPoint + "disconnected");
                    }
                }

                try
                {
                    socket_Connect = socket_Server.Accept();
                    RemoteEndPoint = socket_Connect.RemoteEndPoint.ToString();
                    Console.WriteLine(RemoteEndPoint + " is connected");
                    dic.Add(RemoteEndPoint, socket_Connect);

                    Tablet newTablet = new Tablet();
                    newTablet.ip = RemoteEndPoint;
                    newTablet.status = "Online";
                    tabletList.Add(newTablet);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            Console.WriteLine("end of while");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

static class SocketExtensions
{
    public static bool IsConnected(this Socket socket)
    {
        try
        {
            return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
        }
        catch (SocketException) { return false; }
    }
}

Thanks for help.

Community
  • 1
  • 1
AkariKamigishi
  • 279
  • 7
  • 22

1 Answers1

4

There's a lot of misinformation on that subject, and some of it is present in the questions you linked. The only reliable way to detect a TCP disconnection is to try to write to the connection. A read timeout can also indicate a dropped connection, but it may mean a lot of other things too, such as a stuck server. An EOS condition when reading indicates a graceful disconnect. The IsConnected() method and friends only give you a history of what you have done to this socket: they don't give you the current state of the connection. They can't, because, absent a pending write request, there is no state to know. TCP doesn't maintain anything akin to a dial tone.

user207421
  • 289,834
  • 37
  • 266
  • 440