How to know Socket client got disconnected?
I am doing coding in linux architecture. I have question regarding socket server and client. I have made one sample code in which server continue to accept the connection and client is connected to server. if somehow someone has remove the network cable so i am disconnecting client (client socket disconnected from PC) whi开发者_运维技巧le in server side connection is still alive because i am not able to notify that client is disconnected because network is unplugged.
How can i know that client got disconnected ?
Thanks, Neel
You need to either configure keepalive on the socket or send an application level heartbeat message, otherwise the listening end will wait indefinitely for packets to arrive. If you control the protocol, the application level heartbeat may be easier. As a plus side, either solution will help keep the connection alive across NAT gateways in the network.
See this answer: Is TCP Keepalive the only mechanism to determine a broken link?
Also see this Linux documentation: http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/#programming
SIGPIPE for local sockets and eof on read for every socket type.
精彩评论