Instantly detect client disconnection from server socket

后端 未结 14 1423
北恋
北恋 2020-11-22 09:27

How can I detect that a client has disconnected from my server?

I have the following code in my AcceptCallBack method

static Socket hand         


        
相关标签:
14条回答
  • 2020-11-22 09:37

    Expanding on comments by mbargiel and mycelo on the accepted answer, the following can be used with a non-blocking socket on the server end to inform whether the client has shut down.

    This approach does not suffer the race condition that affects the Poll method in the accepted answer.

    // Determines whether the remote end has called Shutdown
    public bool HasRemoteEndShutDown
    {
        get
        {
            try
            {
                int bytesRead = socket.Receive(new byte[1], SocketFlags.Peek);
    
                if (bytesRead == 0)
                    return true;
            }
            catch
            {
                // For a non-blocking socket, a SocketException with 
                // code 10035 (WSAEWOULDBLOCK) indicates no data available.
            }
    
            return false;
        }
    }
    

    The approach is based on the fact that the Socket.Receive method returns zero immediately after the remote end shuts down its socket and we've read all of the data from it. From Socket.Receive documentation:

    If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

    If you are in non-blocking mode, and there is no data available in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException.

    The second point explains the need for the try-catch.

    Use of the SocketFlags.Peek flag leaves any received data untouched for a separate receive mechanism to read.

    The above will work with a blocking socket as well, but be aware that the code will block on the Receive call (until data is received or the receive timeout elapses, again resulting in a SocketException).

    0 讨论(0)
  • 2020-11-22 09:40

    Since there are no events available to signal when the socket is disconnected, you will have to poll it at a frequency that is acceptable to you.

    Using this extension method, you can have a reliable method to detect if a socket is disconnected.

    static class SocketExtensions
    {
      public static bool IsConnected(this Socket socket)
      {
        try
        {
          return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
        }
        catch (SocketException) { return false; }
      }
    }
    
    0 讨论(0)
  • 2020-11-22 09:43

    This is simply not possible. There is no physical connection between you and the server (except in the extremely rare case where you are connecting between two compuers with a loopback cable).

    When the connection is closed gracefully, the other side is notified. But if the connection is disconnected some other way (say the users connection is dropped) then the server won't know until it times out (or tries to write to the connection and the ack times out). That's just the way TCP works and you have to live with it.

    Therefore, "instantly" is unrealistic. The best you can do is within the timeout period, which depends on the platform the code is running on.

    EDIT: If you are only looking for graceful connections, then why not just send a "DISCONNECT" command to the server from your client?

    0 讨论(0)
  • 2020-11-22 09:43

    The example code here http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx shows how to determine whether the Socket is still connected without sending any data.

    If you called Socket.BeginReceive() on the server program and then the client closed the connection "gracefully", your receive callback will be called and EndReceive() will return 0 bytes. These 0 bytes mean that the client "may" have disconnected. You can then use the technique shown in the MSDN example code to determine for sure whether the connection was closed.

    0 讨论(0)
  • 2020-11-22 09:43

    Can't you just use Select?

    Use select on a connected socket. If the select returns with your socket as Ready but the subsequent Receive returns 0 bytes that means the client disconnected the connection. AFAIK, that is the fastest way to determine if the client disconnected.

    I do not know C# so just ignore if my solution does not fit in C# (C# does provide select though) or if I had misunderstood the context.

    0 讨论(0)
  • 2020-11-22 09:43

    i had same problem , try this :

    void client_handler(Socket client) // set 'KeepAlive' true
    {
        while (true)
        {
            try
            {
                if (client.Connected)
                {
    
                }
                else
                { // client disconnected
                    break;
                }
            }
            catch (Exception)
            {
                client.Poll(4000, SelectMode.SelectRead);// try to get state
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题