If the remote socket is properly closed, then your socket will become readable but the receive call will return no data. If there is an error in the communication, the next receive or send operation will return an error.
However if the connection just goes idle, then you will not know if the other end is alive or not. You can set a socket option (SO_KEEPALIVE
) to automatically have the system send "keep-alive" messages and discover if there is any problems. The problem with SO_KEEPALIVE
is that the default timing for this is two hours. If you need shorter times (you most likely do) then your best bet is to have your own keep-alive messaging implemented as part of the protocol.