ServerSocket 构建失败

发布于 2024-10-31 14:16:34 字数 1167 浏览 4 评论 0原文

我有以下构造:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨与橙与城 2024-11-07 14:16:35

根据 API,BindException

表示发生错误的信号
尝试将套接字绑定到本地
地址和端口。通常,端口
正在使用中,或请求的本地
无法分配地址。

基本上,该端口可能被另一个进程使用,甚至可能由于不正常的关闭而被同一应用程序的另一个实例使用。此链接可以帮助您排除故障。

注意:

运行 netstat -an 命令的发布结果似乎验证了端口 8085 正在被多个进程使用,这将导致您遇到的 BindException

According to the API, a BindException

Signals that an error occurred while
attempting to bind a socket to a local
address and port. Typically, the port
is in use, or the requested local
address could not be assigned.

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 the BindException that you're encountering.

巷子口的你 2024-11-07 14:16:35

如果您将捕获的异常包含在新抛出的异常中,您将获得更详细的异常原因:

try {
    serverSocket = new ServerSocket(port);
} catch (Exception e) {
    throw new RuntimeException("Could not create server socket.", e);
}

不过,我最初的猜测是,如果您已经处理了防火墙问题,则该端口已在使用中。

You'll get a more detailed reason for your exception if you include the caught exception in the newly thrown exception:

try {
    serverSocket = new ServerSocket(port);
} catch (Exception e) {
    throw new RuntimeException("Could not create server socket.", e);
}

My initial guess, though, is that the port is already in use if you've already dealt with firewall issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文