TCP keep-alive to determine if client disconnected in netty

前端 未结 4 580
日久生厌
日久生厌 2020-12-05 11:48

I\'m trying to determine if a client has closed a socket connection from netty. Is there a way to do this?

4条回答
  •  醉梦人生
    2020-12-05 12:10

    If you are writing a server, and netty is your client, then your server can detect a disconnect by calling select() or equivalent to detect when the socket is readable and then call recv(). If recv() returns 0 then the socket was closed gracefully by the client. If recv() returns -1 then check errno or equivalent for the actual error (with few exceptions, most errors should be treated as an ungraceful disconnect). The thing about unexpected disconnects is that they can take a long time for the OS to detect, so you would have to either enable TCP keep-alives, or require the client to send data to the server on a regular basis. If nothing is received from the client for a period of time then just assume the client is gone and close your end of the connection. If the client wants to, it can then reconnect.

提交回复
热议问题