Break through the limit that a program can only open 28232 tcp connection ports in Linux

Source: Internet
Author: User
You may know how to set it, but record the solution. Just look at the last three lines. To test the epoll performance of javamustang (jdk1.6), Tim recently wrote a JavaNIO client to connect to the server in Linux, in principle, a client can open 65535 ports (and only one thread is required. nonbl may know how to set the port, but the solution is recorded. Just look at the last three lines.
To test the epoll performance of java mustang (jdk1.6), Tim recently wrote a JavaNIO client to connect to the server in Linux, in principle, a client can open 65535 ports (and only one thread is required, and nonblocking io is good), but after each Linux client opens the 28231-28233 connection, it will say
Cannot assign requested address
In this case, the same error is reported when you use telnet to connect to the server:
Unable to connect to remote host: Cannot assign requestedaddress
It is not a Java program problem. Use TcpdumpAfter listening, I found no network traffic, which may be the limitation of the local kernel.
Use netstat to check whether all local ports on the client are 30000 ~ About 60000.
Considering the default port limit of 5000 in Windows, I think Linux should also have a limit.
Google's solution on the Internet may fail to find the keyword and no answer (it is difficult to find the answer to the string with an error in the search). I only know that the string is "EADDRNOTAVAIL" in error. h ". I couldn't find a solution, so I had to look at the Linux source code.
Unlock the source code of Linux 2.6. check the implementation of connect () based on the socket interface.
Open tcp_ipv4.c and find
/* This will initiate an outgoing connection .*/
Int tcp_v4_connect (struct sock * sk, struct sockaddr * uaddr, intaddr_len)
It should be here. as you can see, it calls another function.
/*
* Bind a port for a connect operation and hash it.
*/
Static int tcp_v4_hash_connect (struct sock * sk)
This function judges a variable in sysctl_local_port_range. if the variable does not meet the conditions, return-EADDRNOTAVAIL;
The problem is sysctl_local_port_range.
Find the context settings about sysctl_local_port_range.
In tcp. c. The initial value is set here.
Void _ init tcp_init (void)
/* Try to be a bit smarter and adjust defaults depending
* On available memory.
*/
If (order> 4 ){
Sysctl_local_port_range [0] = 32768;
Sysctl_local_port_range [1] = 61000;
Sysctl_tcp_max_tw_buckets = 180000;
Sysctl_tcp_max_orphans = 4096 <(order-4 );
Sysctl_max_syn_backlogs = 1024;
}
Note that the program should be improved to dynamically allocate the opened ports based on the memory, but the current version is dead :(
Definition:
/*
* This array holds the first and last local port number.
* For high-usage systems, use sysctl to change this
* 32768-61000
*/
Int sysctl_local_port_range [2] ={ 1024,499 9 };
Tim's Final Solution: because there is a comment above that we can use sysctl to set it. how can we set it with a line of words?
Echo "net. ipv4.ip _ local_port_range = 1024 65000">/etc/sysctl. conf; sysctl-p
Once done, a client can initiate 65000 connections, and the program writer will face these unexpected problems every day.
Note: The above method is used on Linux 2.6.9. if your kernel version is different and does not take effect, don't come to Tim :)

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.