I\'m trying to determine if a client has closed a socket connection from netty. Is there a way to do this?
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.