Does socket become unusable after connect() fails?
c, linux, network-programming, sockets, tcp
Solution
POSIX 2001 says in an informative section:
If `connect()` fails, the state of the socket is unspecified. Conforming applications should close the file descriptor and create a new socket before attempting to reconnect.
So the passage you quote is congruent with this specification. The fact it works on your machine does not mean your program is portable.
Problem
In Chapter 4, paragraph 4.3 of Steven's "The Socket: Networking API, Third Edition", the author states the following If connect fails, the socket is no longer usable and must be closed. We cannot call connect again on the socket. Does anyone know the reason behind the above statement? In my own experiments, i wrote a simple TCP client, that would run on host A and a simple TCP server, that would run on host B. The TCP client would attempt to connect to the TCP server on Host B forever. So, I started the server on host B. Pulled the network wire from the host. Then I started the client on host A. After about 9 unsuccessful connect attempts on the same socket, I simply plugged the network wire back into the server host. The client connected successfully and happily sends messages at 80K/sec. In yet another experiment, I pulled the wire from the server host, after a initial successful connect and few million messages exchanges after. Then, after few minutes, I connected the wire and message flow resumed on the same socket.