Cannot assign requested address using ServerSocket.socketBind
Asked Answered
T

15

63

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?

Threedimensional answered 22/1, 2012 at 22:5 Comment(3)
The javadoc of BindException states: 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. Are you certain you do not have your program running twice, where the first instance uses the port and the second instance throws the exception since the port is already in use by the first instanceSewage
Sounds like either another copy of your server is already running (double check task manager) or you just killed another version of the server and the socket is "lingering" for a while.Corriveau
See nirlevy.blogspot.co.il/2007/12/… "Cannot assign requested address" means that (in your case) it's probable that "localhost" does not map to a valid ip.Shiv
F
23

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.

Fabron answered 22/1, 2012 at 22:12 Comment(9)
Thanks for anwsers, but before posting I've made sure that nothing blocks port 9999. After restarting my PC there's same error.Threedimensional
Yes. Everything that I run is automatically getting elevated privileges.Threedimensional
Yes, command line is running with administrator privileges. I'm using "java -jar SocketServer.jar" command.Threedimensional
Would someone explain, why the second form is different/better?Ejectment
I was still getting this error; checked the code and realised my IP (the code was in a VM) had changed.Laywoman
This is incorrect. If the port was taken by another process the error would be "java.net.BindException: Address already in use". The error "java.net.BindException: Cannot assign requested address" problem is because you've tried to open a socket on invalid IP address.Rejuvenate
@dan.mwasuser2321368 You're close. It would be 'port already in use'.Crash
@EJP With java version "1.8.0_141" at least, the exception thrown is "java.net.BindException: Address already in use (Bind failed)"Rejuvenate
@EJP Which was also the case when I tested this under Java 7 before posting my comment last year.Rejuvenate
I
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

Indigestion answered 31/10, 2012 at 14:4 Comment(2)
adding this configuration in hosts file saved my day 127.0.0.1 localhost . Thanks!Polyamide
I have 127.0.0.1 localhost in /etc/hosts, yet I am still getting the error (macOS Monterey).Usanis
F
23

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.

Fabron answered 22/1, 2012 at 22:12 Comment(9)
Thanks for anwsers, but before posting I've made sure that nothing blocks port 9999. After restarting my PC there's same error.Threedimensional
Yes. Everything that I run is automatically getting elevated privileges.Threedimensional
Yes, command line is running with administrator privileges. I'm using "java -jar SocketServer.jar" command.Threedimensional
Would someone explain, why the second form is different/better?Ejectment
I was still getting this error; checked the code and realised my IP (the code was in a VM) had changed.Laywoman
This is incorrect. If the port was taken by another process the error would be "java.net.BindException: Address already in use". The error "java.net.BindException: Cannot assign requested address" problem is because you've tried to open a socket on invalid IP address.Rejuvenate
@dan.mwasuser2321368 You're close. It would be 'port already in use'.Crash
@EJP With java version "1.8.0_141" at least, the exception thrown is "java.net.BindException: Address already in use (Bind failed)"Rejuvenate
@EJP Which was also the case when I tested this under Java 7 before posting my comment last year.Rejuvenate
S
16

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)
Sommers answered 2/8, 2013 at 2:4 Comment(3)
it was the same for me aswell. ipadress was changed.Anabasis
thank you, I would have tried to figure this out for years had I not read this.Currish
for me the issue was that previous ip address was still in etc/hostsShushan
V
15

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.

Vedetta answered 10/10, 2013 at 16:13 Comment(0)
J
8

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.

Johann answered 9/7, 2015 at 12:36 Comment(4)
This is a different error than what the question was about.Rejuvenate
The question was about an error message "Cannot assign requested address". That error message is NOT the same as the error message your answer illustrates, or solves.Rejuvenate
This should be the accepted answer. 'Cannot assign requested address error' is due to to the OS being unable to resolve the 'localhost', which is required to start and shutdown the server. This is most commonly caused by a wrongly configured hosts file. This error is NOT particularly due to port being in use. I commented all the entries except this '127.0.0.1 localhost' from my /etc/hosts file to resolve similar issue w/ my tomcat app.Milissa
This solution is not correct. localhost must map to 127.0.0.1, and the 'real' IP address must map to a hostname other than localhost`.Crash
B
7

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.

Biosphere answered 4/1, 2017 at 2:29 Comment(0)
A
4

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

httpclient.localaddress=12.34.56.78
Aho answered 14/8, 2013 at 23:7 Comment(0)
S
1

My laptop has an internal DNS name in the network, it was fine until something and then has broken.

To fix i added a line to route all requests by DNS name to my 127.0.0.1, my /etc/hosts looks like this:

127.0.0.1       localhost
127.0.0.1       host.docker.internal
127.0.0.1       my-url.box #added for the problem

Might be relevant for someone.

It is easy to debug, just run a class until this is green:

public static void main(String[] args) throws Exception {
        new ServerSocket(0, 1, InetAddress.getLocalHost());
    }
Sanches answered 15/6, 2021 at 15:24 Comment(0)
B
1

In my case: Just restarted my computer and everything works fine after that.

Blimp answered 11/8, 2021 at 23:27 Comment(0)
C
0

As the error states, it can't bind - which typically means it's in use by another process. From a command line run:

netstat -a -n -o

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

Cosmetician answered 22/1, 2012 at 22:14 Comment(2)
As the error states, the address isn't available, which means the ports are completely irrelevant.Vedetta
This is incorrect. If the port was taken by another process the error would be "java.net.BindException: Address already in use". The error "java.net.BindException: Cannot assign requested address" problem is because you've tried to open a socket on invalid IP address.Rejuvenate
S
0

In my case, delete from /etc/hosts

  • 127.0.0.1 localhost
  • 192.168.100.20 localhost <<<<---- (delete or comment)
Subtotal answered 30/3, 2016 at 13:52 Comment(0)
S
0

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.

Sometimes answered 16/5, 2016 at 20:16 Comment(0)
M
0

if you happened on CentOS?

You should try to this.

$ service network restart

or

reboot your server.

Mccarron answered 23/8, 2016 at 1:24 Comment(0)
L
-1

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.

Lash answered 22/1, 2012 at 22:7 Comment(2)
It's not possible! I've just restarted my PC, it doesn't help.Threedimensional
This is incorrect. If the port was taken by another process the error would be "java.net.BindException: Address already in use". The error "java.net.BindException: Cannot assign requested address" problem is because you've tried to open a socket on invalid IP address.Rejuvenate
D
-2

java.net.BindException: Cannot assign requested address

According to BindException documentation, it basically:

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.

So try the following command:

sudo lsof -i:8983

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).

Diversified answered 6/4, 2015 at 21:11 Comment(2)
This is incorrect. If the port was taken by another process the error would be "java.net.BindException: Address already in use". The error "java.net.BindException: Cannot assign requested address" problem is because you've tried to open a socket on invalid IP address.Rejuvenate
@dan.mwasuser2321368 Thanks for the suggestion, I've improved my answer, please check my edit.Diversified

© 2022 - 2024 — McMap. All rights reserved.