How can I modify Linux kernel parameters to reduce the TIME-WAIT in TCP connections ?, Linuxtime-wait

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.