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
String myHost = "10.0.0.4"(the Ip I get when runningifconfig). In that case I getjava.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)String myHost = "publichost", and I add a line10.0.0.4 publichostto my/etc/hosts/file. In that case I getjava.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?
sudo netstat -naptu | grep LISTEN | grep <port>ping localhostandping publichost. See what ip it shows. I guess your pc ip address is not set to "10.0.0.4".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.