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?
According to
BindException
documentation, it basically:So try the following command:
to double check if any application is using the same port and kill it.
If that's not the case, make sure that your IP address to which you're trying to bind is correct (it's correctly assigned to your network interface).
The error says
Cannot assign requested address
. This means that you need to use the correct address for one of your network interfaces or0.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.
As the error states, it can't bind - which typically means it's in use by another process. From a command line run:
Interrogate the output for port 9999 in use in the left hand column.
For more information: http://www.zdnetasia.com/see-what-process-is-using-a-tcp-port-62047950.htm
The port is taken by another process. Possibly an unterminated older run of your program. Make sure your program has exited cleanly or kill it.
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.
I came across this error when copying configurations from one server to another.
I had the old host's hostname in my ${JETTY_BASE}/start.ini jetty.host property. Setting the correct jetty.host property value solved the issue for me.
Hope this helps someone in the future who has to work on multiple servers at once.