Completely solve the problem of insufficient TCP client ports

Source: Internet
Author: User
Completely solve the problem of insufficient TCP client ports [problem] when making large concurrent performance calls at www.2cto.com, we often encounter a problem: the client port is exhausted. [Cause] after analysis, the main cause is that there will be a timewait time after the connection is disconnected and the socket ,...
Completely solve the problem of insufficient TCP client ports [problem] when making large concurrent performance calls at www.2cto.com, we often encounter a problem: the client port is exhausted. [Cause] after analysis, the main cause is that there will be a timewait time after the connection is disconnected, and the socket will be retained and occupied. [Solution] solution 1 is always written on timewait at first, and wants to minimize the influence of timewait: 1. modify/proc/sys/net/ipv4/tcp_fin_timeout2, modify/proc/sys/net/ipv4/tcp_max_tw_buckets, and so on. Solution 2: The problem may be mitigated, but it cannot always be eliminated. By chance, I heard something from my friends that could support "millions of concurrent connections" on a single machine, so I thought about how to solve the problem of insufficient tcp client ports. Because the default port 65535 is configured and the maximum number of ports is less than 60000, how can it reach a million? After reading UNP, I have mentioned four scenarios of the SO_REUSEADDR application: SO_REUSEADDR allows a single process to bind the same port to multiple sockets, as long as a different Betty IP address is specified each time. So I guess: when the port of an IP address is exhausted, I can select another IP address to send messages. First, write the following code for verification: Python code from socket import * from time import * s1 = socket (AF_INET, SOCK_STREAM) # s1.setsockopt (SOL_SOCKET, SO_REUSEADDR, 1) s1.bind ('2017. 168.0.35 ', 5555) s1.connect ('2017. 0.0.1 ', 1337) print 'etet1 connect OK' s2 = socket (AF_INET, SOCK_STREAM) # s2.setsockopt (SOL_SOCKET, SO_REUSEADDR, 1) s2.bind ('192. 168.1.105 ', 5555) s2.connect ("127. 0.0.1 ', 1337) print 'etet2 connect OK 'sleep (4) indicates that the environment: 192.168.0.35 and 192.168.1.105 are local IP addresses, and a TCP server is set up on port 1337. Result: s1 and s2 are successfully connected. Cmd code C: \ Documents and Settings \ Administrator> netstat-an | grep 1337 TCP 0.0.0.0: 1337 0.0.0.0: 0 listening tcp 192.168.0.35: 1337 192.168.0.35: 7780 established tcp 192.168.0.35: 5555 127.0.0.1: 1337 TIME_WAIT TCP 192.168.0.35: 7780 192.168.0.35: 1337 established tcp 192.168.1.105: 5555 127.0.0.1: 1337 TIME_WAIT the solution is feasible.
Related Article

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.