From: http://hi.baidu.com/zknehycmrobrtvd/item/12fdf54dcae951e01381da92
You may know how to set it, but record the solution. Just look at the last three lines.
To test the epoll performance of Java Mustang (jdk1.6), Tim recently wrote a Java NiO 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 requested address
It is not a Java program problem. I listened with tcpdump and found that there is no network traffic, which may be a limitation on 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, int addr_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 preceding method is used on Linux 2.6.9.
2008/8/24 supplement:
Remove time wait. On the other hand, the port usage is too large.
# Disable time_wait... wait ..
Net. ipv4.tcp _ tw_recycle = 1