How to detect CLOSE_WAIT on socket in C on Linux?

2.6k Views Asked by At

I'm writing TCP client and I want to reconnect immediately when connection will be broken (i.e. server is restarted). I want to restart connection as soon as possible, even if there is no data to send over this socket. I see in netstat that connection is in CLOSE_WAIT state, but there is no error from select, getsockopt or getpeername. I know that I can try to write something to socket, but this connection is only for reading.

1

There are 1 best solutions below

1
On

From man recv:

Return Value

These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.