Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I implement my own TCP/IP socket timeouts?

The software I'm working on needs to be able to connect to many servers in a short period of time, using TCP/IP. The software runs under Win32. If a server does not respond, I want to be able to quickly continue with the next server in the list.

Sometimes when a remote server does not respond, I get a connection timeout error after roughly 20 seconds. Often the timeout comes quicker.

My problem is that these 20 seconds hurts the performance of my software, and I would like my software to give up sooner (after say 5 seconds). I assume that the TCP/IP stack (?) in Windows automatically adjusts the timeout based on some parameters?

Is it sane to override this timeout in my application, and close the socket if I'm unable to connect within X seconds?

(It's probably irrelevant, but the app is built using C++ and uses I/O completion ports for asynchronous network communication)

like image 661
Nitramk Avatar asked Oct 14 '09 19:10

Nitramk


People also ask

What is a good socket timeout?

Best Answer. Given that on Policy server the default idle timeout for socket is 10min, I think 10 min is good vlaue for it.

What is TCP socket timeout?

TCP Socket Timeouts are caused when a TCP socket times out talking to the far end. Socket timeouts can occur when attempting to connect to a remote server, or during communication, especially long-lived ones.

How long does a TCP connection last?

Once a TCP connection has been established, that connection is defined to be valid until one side closes it. Once the connection has entered the connected state, it will remain connected indefinitely.

Does socket timeout close socket?

If the socket timeout is not programmed, then the socket will remain open as it waits for the other side to connect. Allowing it to remain open opens the computer up to potential malicious attacks; more commonly, the computer just uses excess memory to connect to a network that is not responding.


2 Answers

If you use IO completion ports and async operations, why do you need to wait for a connect to complete before continuing with the next server on the list? Use ConnectEx and pass in an overlapped structure. This way the individual server connect time will no add up, the total connect time is the max server connect time not the sum.

like image 195
Remus Rusanu Avatar answered Sep 23 '22 14:09

Remus Rusanu


On Linux you can

int syncnt = 1;
int syncnt_sz = sizeof(syncnt);
setsockopt(sockfd, IPPROTO_TCP, TCP_SYNCNT, &syncnt, syncnt_sz);

to reduce (or increase) the number of SYN retries per connect per socket. Unfortunately, it's not portable to Windows.

As for your proposed solution: closing a socket while it is still in connecting state should be fine, and it's probably the easiest way. But since it sounds like you're already using asynchronous completions, can you simply try to open four connections at a time? If all four time out, at least it will only take 20 seconds instead of 80.

like image 21
ephemient Avatar answered Sep 21 '22 14:09

ephemient