I am writing a client which connects the server through TCP and sends
some data periodically. The server may drop the connection and the
client should re-establish the connection before next Write().
This is a very typical program and I thought I could detect the closed
connection by checking the returned error from Write(). I wrote two
simple programs to see what error will be returned from Write() if the
connection is closed by the remote end. (It should be EPIPE according to
However, the Write() operation on the closed connection returns no
error. I would like to know how can I detect the closed connection in
Here are the test programs I wrote:
- client program:
The logic is very simple: connect to server, wait for a second, then
write some data and print the return values from Write().
- server program:
Still very simple: listen on a port, if there is a connection, accept it
and close it immediately.