“java.net.BindException: Address already in use” 当我已经在同一个端口上运行服务时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20675472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:31:08  来源:igfitidea点击:

"java.net.BindException: Address already in use" when I am already running a service on the same port

javaserversocket

提问by user2346809

I have one server socket program, when I run this program I get the following error:

我有一个服务器套接字程序,当我运行这个程序时,我收到以下错误:

java.net.BindException: Address already in use
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.<init>(ServerSocket.java:194)
    at Server.main(Server.java:20)
Could not listen on port: 5434.

A postgres sql server is already running on this port, which explains the error. My hardware device only sends data to this 5434 port.

postgres sql server 已经在这个端口上运行,这解释了错误。我的硬件设备只向这个 5434 端口发送数据。

What are my options to get around this error?

我有哪些选择可以解决此错误?

回答by Scary Wombat

If the port is being used by postgresql then you cant also open this port as a server port. Either shut postgresql down or use another port.

如果 postgresql 正在使用该端口,那么您也不能将此端口作为服务器端口打开。关闭 postgresql 或使用另一个端口。

回答by Eric Leschinski

Find out what program is using your port:

找出正在使用您的端口的程序:

sudo netstat -nlp | grep 514

udp        0      0 :::514                :::*                            30186/java

Tells me that a java program is binding on UDP port 514

告诉我一个 java 程序绑定在 UDP 端口 514

回答by neoneye

Mac specific

Mac 专用

An instance of my java program was already running and occupying the port. So when I tried starting a new instance then I got the error. In order to kill the old instance I do the following:

我的 java 程序的一个实例已经在运行并占用了端口。所以当我尝试启动一个新实例时,我得到了错误。为了杀死旧实例,我执行以下操作:

prompt> lsof -i:5434
COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    71107 neoneye   68u  IPv6 0x4a0371dea87d2cd3      0t0  TCP *:http-alt (LISTEN)
prompt> kill 71107

btw. I don't recommend stopping postgresql this way.

顺便提一句。我不建议以这种方式停止 postgresql。

回答by java.nazif

You should change your port number. I had the same error. After I changed my port number, it resolved.

您应该更改您的端口号。我有同样的错误。更改端口号后,它解决了。