
Jonathan Franklin
At the very least, this is a technique I have consistently seen recommended, and has always worked for me.
The only reliable way to detect the remote host dropping off, is to attempt a read. This is the consistent recommendation by socket programming experts. Please refer to _Effective TCP/IP Programming_, Tip 19; and Stevens.
Sure, I agree completely that attempting a read is necessary, but IME it is not sufficient. You must additionally send data, either with an OS write or TCP keepalive, to detect a completely unresponsive peer (i.e. one which has fallen off the network). The only way to detect an unresponsive peer is via a timeout, and with no data to send, there is nothing to time out. I also agree that write() won't always return an error, but it should attempt to send data, which will cause the TCP layer to wait for an acknowledgement of that data. If that times out, the TCP layer should detect an error on the socket, and a subsequent call to read() should return an error. ----Scott.