ServerSocket 构建失败
我有以下构造:
try {
serverSocket = new ServerSocket(port);
} catch (Exception e) {
throw new RuntimeException("Could not create server socket.");
}
并且构造失败,并且出现“无法创建服务器套接字”错误。端口始终为 8085。我在 (Windows 7) 防火墙中打开了出站和入站端口。可能是什么问题?
例外是 IOException:
线程“main”中出现异常java.lang.RuntimeException:无法创建服务器套接字。 在cs236369.proxy.ProxyServer。(ProxyServer.java:23) 在cs236369.proxy.ProxyServer.main(ProxyServer.java:62)
处运行 netstat -an
给出以下结果:
TCP 0.0.0.0:8085 0.0.0.0:0 LISTENING
TCP [::]:8085 [::]:0 LISTENING
堆栈跟踪:
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at cs236369.proxy.ProxyServer.<init>(ProxyServer.java:21)
at cs236369.proxy.ProxyServer.main(ProxyServer.java:63)
I have the following construction:
try {
serverSocket = new ServerSocket(port);
} catch (Exception e) {
throw new RuntimeException("Could not create server socket.");
}
and the construction fails and I get the "Could not create server socket" error. The port is always 8085. I opened both out-bounding and in-bounding ports in my (Windows 7) firewall. What could be the problem?
The exception is IOException:
Exception in thread "main" java.lang.RuntimeException: Could not create server socket.
at cs236369.proxy.ProxyServer.(ProxyServer.java:23)
at cs236369.proxy.ProxyServer.main(ProxyServer.java:62)
Running netstat -an
gives the following result:
TCP 0.0.0.0:8085 0.0.0.0:0 LISTENING
TCP [::]:8085 [::]:0 LISTENING
Stack Trace:
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at cs236369.proxy.ProxyServer.<init>(ProxyServer.java:21)
at cs236369.proxy.ProxyServer.main(ProxyServer.java:63)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 API,BindException
基本上,该端口可能被另一个进程使用,甚至可能由于不正常的关闭而被同一应用程序的另一个实例使用。此链接可以帮助您排除故障。
注意:
运行
netstat -an
命令的发布结果似乎验证了端口 8085 正在被多个进程使用,这将导致您遇到的BindException
。According to the API, a BindException
Basically, that port could be in use by another process, even perhaps by another instance of the same application due to an unclean shutdown. This link may help you troubleshoot.
Note:
The posted results of running the
netstat -an
command seems to verify that port 8085 is in use by multiple processes, which would result in theBindException
that you're encountering.如果您将捕获的异常包含在新抛出的异常中,您将获得更详细的异常原因:
不过,我最初的猜测是,如果您已经处理了防火墙问题,则该端口已在使用中。
You'll get a more detailed reason for your exception if you include the caught exception in the newly thrown exception:
My initial guess, though, is that the port is already in use if you've already dealt with firewall issues.