How can I modify Linux kernel parameters to reduce the TIME-WAIT in TCP connections ?, Linuxtime-wait
I. Basic settings
If the address bound to the socket in python is in use, errors may occur,
In linux:
"Socket. error: [Errno 98] Address already in use" is displayed"
In windows:
"Socket. error: [Errno 10048] is displayed. Generally, each socket address (Protocol/network address/port) can be used only once"
This is because the socket does not support address multiplexing by default. If you want to reuse the socket, You need to display the settings, that is, call the setsockop function t before binding to allow the socket to reuse the address: socket. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
For example:
[Python] view plain copy
Self. recSocket = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) self. recSocket. settimeout (CHECK_TIMEOUT)
Self. recSocket. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1) self. recSocket. bind ('', UDP_PORT ))
Ii. Further settings (you need to set a large number of concurrent connections)
The above is to set reuse in the program. In addition, if there are too many concurrent connection requests, that is, there are many connection requests in a short time, and the system will automatically release the occupied port for a while, there are connection requests (available ports have been used up), so the Address already in use error prompt will appear), a large number of connections in the TIME_WAIT status will be generated. In this case, it is necessary to adjust the Linux TCP/IP kernel parameters so that the system can release the TIME_WAIT connection more quickly.
Open the configuration file with vi:
[Python] view plain copy
# Vi/etc/sysctl. conf
Then, add the following lines to the file (for details and explanations, see the article modify linux kernel parameters to reduce the TIME-WAIT and linux TCP connection configurations in TCP connections ):
[Python] view plain copy
Net. ipv4.tcp _ syncookies = 1 # If this configuration file exists, you do not need to add net. ipv4.tcp _ tw_reuse = 1.
Net. ipv4.tcp _ tw_recycle = 1net. ipv4.tcp _ fin_timeout = 5
Finally, enter the following command to make the kernel parameters take effect:
[Python] view plain copy
#/Sbin/sysctl-p