Cannot assign requested address: JVM_Bind

2019-01-07 11:23发布

When I'm trying to set up a socket server, I've got an error message:

Exception in thread "main" java.net.BindException: Cannot assign requested address: JVM_Bind
    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 java.net.ServerSocket.<init>(ServerSocket.java:106)
    at socketyserver.SocketyServer.main(SocketyServer.java:12)
Java Result: 1

Whole code is simplest as it can be:

public static void main(String[] args) throws UnknownHostException, IOException
{
    ServerSocket serverSocket;
    serverSocket = new ServerSocket(9999);
}

I'm 100% sure that my ports are forwarded, Windows Firewall is off. Nothing blocks port 9999. What else can go wrong?

13条回答
姐就是有狂的资本
2楼-- · 2019-01-07 11:35

For me it was because a previous jmeter.properties change was still in play

httpclient.localaddress=12.34.56.78
查看更多
干净又极端
3楼-- · 2019-01-07 11:37

if you happened on CentOS?

You should try to this.

$ service network restart

or

reboot your server.

查看更多
Evening l夕情丶
4楼-- · 2019-01-07 11:38

It may be related to a misconfiguration in your /etc/hosts. In my case, it was like this: 192.168.1.11 localhost instead of 127.0.0.1 localhost

查看更多
Melony?
5楼-- · 2019-01-07 11:39

if your are using server, there's "public network IP" and "internal network IP". Use the "internal network IP" in your file /etc/hosts and "public network IP" in your code. if you use "public network IP" in your file /etc/hosts then you will get this error.

查看更多
Viruses.
6楼-- · 2019-01-07 11:46

Java documentation for java.net.BindExcpetion,

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.

Cause:

The error is due to the second condition mentioned above. When you start a server(Tomcat,Jetty etc) it listens to a port and bind a socket to an address and port. In Windows and Linux the hostname is resolved to IP address from /etc/hosts This host to IP address mapping file can be found at C:\Windows\System32\Drivers\etc\hosts. If this mapping is changed and the host name cannot be resolved to the IP address you get the error message.

Solution:

Edit the hosts file and correct the mapping for hostname and IP using admin privileges.

eg:

#127.0.0.1 localhost
192.168.52.1 localhost

Read more: java.net.BindException : cannot assign requested address.

查看更多
姐就是有狂的资本
7楼-- · 2019-01-07 11:46

In my case, delete from /etc/hosts

  • 127.0.0.1 localhost
  • 192.168.100.20 localhost <<<<---- (delete or comment)
查看更多
登录 后发表回答