0

I have implemented a server application in Java that I am trying to deploy in the cloud. I have a problem with this part of the code

serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(myHost,myPort));

When I set String myHost = "localhost", everything works fine. However, I would like to it to work with the public Ip of the remote machine. I have tried 2 different things

  1. String myHost = "10.0.0.4" (the Ip I get when running ifconfig). In that case I get

    java.net.BindException: Cannot assign requested address
         at sun.nio.ch.Net.bind0(Native Method)
         at sun.nio.ch.Net.bind(Net.java:433)
         at sun.nio.ch.Net.bind(Net.java:425)
         at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
         at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
         at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
    
  2. String myHost = "publichost", and I add a line 10.0.0.4 publichost to my /etc/hosts/ file. In that case I get

    java.net.SocketException: Unresolved address
        at sun.nio.ch.Net.translateToSocketException(Net.java:131)
        at sun.nio.ch.Net.translateException(Net.java:157)
        at sun.nio.ch.Net.translateException(Net.java:163)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:76)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
    

What I am doing wrong?

Raphael D.
  • 778
  • 2
  • 7
  • 18
  • 1
    May be try bind 0.0.0.0? Any way you wan't to listen on public address, then bind all interfaces. Also make sure that, there is no other application listening on same port. Probably check with: `sudo netstat -naptu | grep LISTEN | grep ` – muradm Oct 30 '18 at 13:18
  • did you try `ping localhost` and `ping publichost`. See what ip it shows. I guess your pc ip address is not set to "10.0.0.4". – MD Ruhul Amin Oct 30 '18 at 13:36
  • @muradm binding 0.0.0.0 works, thanks for the tip. However, I'd still like to solve this issue, that could be useful in case I want to use the private cloud Ip instead of the public one. – Raphael D. Oct 30 '18 at 14:15
  • @RaphaelD. you can look also for ipv4/ipv6 default preference in jvm, it is flag to `java`, new versions as far as I remember could have problems on some OS configurations with ipv4/ipv6.. forcing it to ipv4 was solving such issues. This one I think: `-Djava.net.preferIPv4Stack=true`. Still it can also be that, the port you are trying to bind is actually busy. – muradm Oct 30 '18 at 14:18

1 Answers1

2

The first error (typically) means that you are binding to an IP + port combination that is already in use.

Use netstat -lntp to list all of the programs listening on a tcp port, and look for the port you are trying to use. Then either shutdown the program ... or pick a different port.

It might also mean that you are using the wrong IP entirely. When you call bind on a server socket, the address and port should be the IP and port on which your application expects to receive incoming connections. So the IP must be an IP for this host (NOT the remote host). Note that you can also use 0.0.0.0 ... which means "all IP addresses for this host".

The second error could mean:

  • Your DNS resolver is not looking at your "/etc/hosts" file.
  • The /etc/hosts entry is incorrect; you are supposed to put the fully qualified name for your host into the entry; see Fully qualified machine name Java with /etc/hosts
  • Something else.

But I suspect that if you fixed the "Unresolved address" problem without fixing the cause of the original "Cannot assign requested address", the latter would reappear. You shouldn't need a DNS entry to bind a server socket!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I will focus on the first issue for now then, thanks for the advice. Concerning your first point, it seems to me that when I use twice the same address, I get a different exception, namely `java.net.BindException: Address already in use`. That would point out to the second explanation. The fact that binding 0.0.0.0 works suggests that there is a problem with the IP. But why is that 10.0.0.4 does not work, while it is shown by ifconfig? – Raphael D. Oct 30 '18 at 14:19
  • Yes ... but which IP is 10.0.0.4? The IP of the host you are running this Java code on? – Stephen C Oct 30 '18 at 14:21
  • Yes. Maybe this is linked to an IPv4/IPv6 issue as mentioned in the comments above. – Raphael D. Oct 30 '18 at 14:35