Instantly detect client disconnection from server socket

后端 未结 14 1623
北恋
北恋 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

    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.

提交回复
热议问题