What is correct procedure following a failure to connect a TCP socket?

628 views Asked by At

I'm writing a TCP client using asynchronous calls. If the server is active when the app starts then it connects and talks OK. However if the first connect fails, then every subsequent call to connect() fails with WSAENOTCONN(10057) without producing any network traffic (checked with Wireshark).

Currently the code does not close the socket when it gets the error. The TCP state diagram does not seem to require it. It simply waits for 30 seconds and tries the connect() again.

That subsequent connect() and the following read() both return the WSAENOTCONN error.

Do I need to close the socket and open a new one? If so, which errors require me to close the socket, since there are a lot of different errors, some of which I will probably never see on the test bench.

You can assume this is MS Winsock2, although it is actually Interval Zero RTX 2009, which is subtly different in some places.

2

There are 2 answers

1
Prabhu On

Do I need to close the socket and open a new one?

Yes.

You should close the socket under all error conditions that results in connection gone for good (Say, like the peer has closed the connection)

3
user207421 On

Do I need to close the socket and open a new one?

Yes.

If so, which errors require me to close the socket, since there are a lot of different errors, some of which I will probably never see on the test bench.

Almost all errors are fatal to the connection and should result in you closing the socket. EAGAIN/EWOULDBLOCK s a prominent exception, as is EINTR, but I can't think of any others offhand.