【发布时间】:2015-08-07 11:17:15
【问题描述】:
我正在运行以下代码,它只是在无限循环中连接和关闭套接字:
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
public class Main {
public static void main(String[] args) throws Exception {
Thread.sleep(1000);
InetAddress localhost = InetAddress.getByName("127.0.0.1");
InetSocketAddress localhostRpcbind = new InetSocketAddress(localhost, 111);
SelectorProvider selectorProvider = SelectorProvider.provider();
long iterations = 0;
while (true) {
try {
SocketChannel socketChannel = selectorProvider.openSocketChannel();
socketChannel.connect(localhostRpcbind);
socketChannel.finishConnect();
socketChannel.close();
iterations ++;
} catch (Exception e) {
System.err.println("after " + iterations + " iterations");
e.printStackTrace(System.err);
throw e;
}
}
}
}
端口 111 是 rpcbind 的端口(它已在我的机器上启动并运行)。 在第一次运行代码时,我会得到类似:
after 28239 iterations
java.net.BindException: Cannot assign requested address
at sun.nio.ch.Net.connect0(Native Method)
at sun.nio.ch.Net.connect(Net.java:458)
at sun.nio.ch.Net.connect(Net.java:450)
at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
at Main.main(Main.java:16)
后续运行将立即失败(0 次迭代),直到一段时间后我再次获得第一个结果(~26-28k 次迭代然后失败)。
发生了什么事,我怎样才能让这个连接/断开循环无限期地正常运行?
我在 linux x64 (fedora 22) 上运行。
注意:是的,我知道代码是无用的,什么也不做,这是我正在尝试调查的更大问题的 SSCCE。
更新 - 我的机器上似乎遇到了临时端口耗尽:
$ cat /proc/sys/net/ipv4/ip_local_port_range
32768 61000
所以我有大约 28k 临时端口用于连接,这与我的错误相符
【问题讨论】:
-
我猜这个问题与 Unix/Linux 比 Java 更相关。我建议添加这些标签。
-
几年前我也遇到过类似的问题。与文件描述符的最大数量和套接字停留时间有关。