我得到了BindException异常,而重用相同的Address.Following是我的代码。
in openConnection方法:
69. Selector selector = SelectorProvider.provider().openSelector();
70. SocketChannel socketChannel = SocketChannel.open();
71. socketChannel.bind(new InetSocketAddress(port));// Edited
72. socketChannel.socket().setReuseAddress(true);
73. socketChannel.configureBlocking(false);
74. socketChannel.connect(remoteAddress);异常:
java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:414)
at sun.nio.ch.Net.bind(Net.java:406)
at sun.nio.ch.SocketChannelImpl.bind(SocketChannelImpl.java:580)
at sun.nio.ch.SocketAdaptor.bind(SocketAdaptor.java:135)
at com.example.client.request.Client.openConnection(Client.java:72)编辑
我解决了InvalidArgument异常,我编辑了上面的文章,但是现在在同一个端口上重新连接时,我得到了上面的exception.Is,我做错了什么?
发布于 2014-09-03 12:33:51
如果您想重用一个地址,则必须在绑定套接字之前调用setReuseAddress(真)。
发布于 2014-09-03 12:03:54
试图将套接字绑定到本地地址和端口时发生错误的信号。通常,端口正在使用,或者请求的本地地址无法分配。
当您启动web服务器或应用服务器(通常在端口上侦听)(例如Tomcat或Jetty在8080和8084上侦听HTTP和HTTPS流量)时,它们会将套接字绑定到本地地址和端口。如果您给它们指定主机名(例如localhost或devhost ),那么它们在Windows和Linux中都使用/etc/host将域名解析为IP地址,如果映射不正确,您将得到java.net.BindException: than分配请求的地址: JVM_Bind。此主机到IP地址映射文件可以在C:\Windows \ system 32\Driver\etc\ host上找到,其中C:\ windows是安装windows操作系统的地方。如果您查看这个文件,您将看到它包含IP地址和主机名,如下所示:
127.0.0.1本地主机
192.168.52.1本地主机
只需更正映射,或在localhost上添加127.0.0.1以解决此问题
https://stackoverflow.com/questions/25643070
复制相似问题