Instantly detect client disconnection from server socket

后端 未结 14 1425
北恋
北恋 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:47

    This worked for me, the key is you need a separate thread to analyze the socket state with polling. doing it in the same thread as the socket fails detection.

    //open or receive a server socket - TODO your code here
    socket = new Socket(....);
    
    //enable the keep alive so we can detect closure
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
    
    //create a thread that checks every 5 seconds if the socket is still connected. TODO add your thread starting code
    void MonitorSocketsForClosureWorker() {
        DateTime nextCheckTime = DateTime.Now.AddSeconds(5);
    
        while (!exitSystem) {
            if (nextCheckTime < DateTime.Now) {
                try {
                    if (socket!=null) {
                        if(socket.Poll(5000, SelectMode.SelectRead) && socket.Available == 0) {
                            //socket not connected, close it if it's still running
                            socket.Close();
                            socket = null;    
                        } else {
                            //socket still connected
                        }    
                   }
               } catch {
                   socket.Close();
                } finally {
                    nextCheckTime = DateTime.Now.AddSeconds(5);
                }
            }
            Thread.Sleep(1000);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:47

    Using the method SetSocketOption, you will be able to set KeepAlive that will let you know whenever a Socket gets disconnected

    Socket _connectedSocket = this._sSocketEscucha.EndAccept(asyn);
                    _connectedSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
    

    http://msdn.microsoft.com/en-us/library/1011kecd(v=VS.90).aspx

    Hope it helps! Ramiro Rinaldi

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

    Someone mentioned keepAlive capability of TCP Socket. Here it is nicely described:

    http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html

    I'm using it this way: after the socket is connected, I'm calling this function, which sets keepAlive on. The keepAliveTime parameter specifies the timeout, in milliseconds, with no activity until the first keep-alive packet is sent. The keepAliveInterval parameter specifies the interval, in milliseconds, between when successive keep-alive packets are sent if no acknowledgement is received.

        void SetKeepAlive(bool on, uint keepAliveTime, uint keepAliveInterval)
        {
            int size = Marshal.SizeOf(new uint());
    
            var inOptionValues = new byte[size * 3];
    
            BitConverter.GetBytes((uint)(on ? 1 : 0)).CopyTo(inOptionValues, 0);
            BitConverter.GetBytes((uint)keepAliveTime).CopyTo(inOptionValues, size);
            BitConverter.GetBytes((uint)keepAliveInterval).CopyTo(inOptionValues, size * 2);
    
            socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
        }
    

    I'm also using asynchronous reading:

    socket.BeginReceive(packet.dataBuffer, 0, 128,
                        SocketFlags.None, new AsyncCallback(OnDataReceived), packet);
    

    And in callback, here is caught timeout SocketException, which raises when socket doesn't get ACK signal after keep-alive packet.

    public void OnDataReceived(IAsyncResult asyn)
    {
        try
        {
            SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
    
            int iRx = socket.EndReceive(asyn);
        }
        catch (SocketException ex)
        {
            SocketExceptionCaught(ex);
        }
    }
    

    This way, I'm able to safely detect disconnection between TCP client and server.

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

    You can also check the .IsConnected property of the socket if you were to poll.

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

    Implementing heartbeat into your system might be a solution. This is only possible if both client and server are under your control. You can have a DateTime object keeping track of the time when the last bytes were received from the socket. And assume that the socket not responded over a certain interval are lost. This will only work if you have heartbeat/custom keep alive implemented.

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

    "That's just the way TCP works and you have to live with it."

    Yup, you're right. It's a fact of life I've come to realize. You will see the same behavior exhibited even in professional applications utilizing this protocol (and even others). I've even seen it occur in online games; you're buddy says "goodbye", and he appears to be online for another 1-2 minutes until the server "cleans house".

    You can use the suggested methods here, or implement a "heartbeat", as also suggested. I choose the former. But if I did choose the latter, I'd simply have the server "ping" each client every so often with a single byte, and see if we have a timeout or no response. You could even use a background thread to achieve this with precise timing. Maybe even a combination could be implemented in some sort of options list (enum flags or something) if you're really worried about it. But it's no so big a deal to have a little delay in updating the server, as long as you DO update. It's the internet, and no one expects it to be magic! :)

    0 讨论(0)
提交回复
热议问题