Cannot assign requested address using ServerSocket.socketBind
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
As other people have pointed out, it is most likely related to another process using port 9999
. On Windows, run the command:
netstat -a -n | grep "LIST"
And it should list anything there that's hogging the port. Of course you'll then have to go and manually kill those programs in Task Manager. If this still doesn't work, replace the line:
serverSocket = new ServerSocket(9999);
With:
InetAddress locIP = InetAddress.getByName("192.168.1.20");
serverSocket = new ServerSocket(9999, 0, locIP);
Of course replace 192.168.1.20
with your actual IP address, or use 127.0.0.1
.
Just for others who may look at this answer in the hope of solving a similar problem, I got a similar message because my ip address changed.
java.net.BindException: Cannot assign requested address: bind
at sun.nio.ch.Net.bind(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:126)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:182)
at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:311)
at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:260)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
at org.eclipse.jetty.server.Server.doStart(Server.java:273)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
The error says Cannot assign requested address
. This means that you need to use the correct address for one of your network interfaces or 0.0.0.0
to accept connections from all interfaces.
The other solutions about ports only work after sometimes-failing black magic (like working after some computer restarts but not others) because the port is completely irrelevant.