Instantly detect client disconnection from server socket

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

提交回复
热议问题