TCP connection timeout

Whether you use any language or library network, you can set the timeout network operations, particularly connect, timeout read, write of.

 

You can set the timeout value in the code of any size, but the connect method will be a little special.
Maximum connect time-out period on any kernel implementations have a can set the timeout value set in your code and can not exceed the maximum value of the maximum value (even if you set the value exceeds its maximum timeout will still be in time after time out).
 
Tcp connection process to establish a syn packet start from the client. If the client does not receive a response to this syn packet, the kernel will retry repeatedly sending the syn packets, each retry interval will be increased gradually to avoid sending too much influence syn packet network.
All kernel syn packet transmitting the number of repetitions has an upper limit. In BSD-derived kernel (including Mac OS X), a second retry 6 seconds after the first transmission syn packet, the third attempt in 18 seconds after the second attempt, if the third further It can not be considered successful connect timeout (this time after 75 seconds).
However, on linux system, the whole event procedure syn packet was sent almost 20 seconds. linux system dense than the interval for such systems bsd syn packet retransmission time. linux syn packet transmission 5 (including the original packet and the retransmitted packet syn later) within 20 seconds, which in turn transmitted the first packet of 3s, 6s, 12s, 24s after transmission.
connect timeout if your program is set smaller than the 20s, so there is no problem. But if you set a value greater than 20s, then you will find that the value of the kernel will be truncated to 20s.
 
Change the upper limit of the system is relatively easy, due to the need to change the system configuration parameters, you need root privileges.
The relevant command is sysctl net.ipv4.tcp_syn_retries (specific to ipv4).
When this value is set to be more conservative, because each interval syn packet retries are increasing (BSD-based systems, such implementation will increase the interval of 2 to 3 times), a slight change in the connect timeout all tcp_syn_retries effects of time are very large, but the expansion of this value do no harm, because the timeout value set in code you can enter into force. But if you do not set timeout values ​​connect code, then connect it will block for a long time, you find that the remote machine is down even longer intervals.
 
Check the value of the command is:
sysctl net.ipv4.tcp_syn_retries
 
4, timeout is 31 seconds.
5, timeout is 75 seconds.
6, timeout is 127 seconds.
 
Default:
net.ipv4.tcp_syn_retries = 4
 
You can modify this value the following command:
sysctl -w net.ipv4.tcp_syn_retries=5
 
If you want to take effect after the restart, the net.ipv4.tcp_syn_retries = 4 placed in /etc/sysctl.conf.

Guess you like

Origin www.cnblogs.com/gaoshaonian/p/11685030.html