Search code examples
javasocketswebsockettcp

Can't Bind TCP Port


I need to establish a TCP connection between my development machine and another type of server machine. Both are inside the same router, under the same public IP.

When I try to create a TCP connection between these devices using my router's public IP, I'm unable to establish the connection. The connection attempt fails.

I have configured everything in my router, and I've tested port forwarding with a simple Python TCP script using the public IP. It works properly! Additionally, when I use a private IP, the TCP connection is established without any issues.

Development Machine : MacBook

Code :

new InetSocketAddress(serverIpAddress, serverTcpPort)

Exception :

Bind failed for TCP channel on endpoint [/<My Public Ip>:8132]
java.net.BindException: Can't assign requested address
    at java.base/sun.nio.ch.Net.bind0(Native Method)
    at java.base/sun.nio.ch.Net.bind(Net.java:459)
    at java.base/sun.nio.ch.Net.bind(Net.java:448)
    at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
    at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
    at akka.io.TcpListener.liftedTree1$1(TcpListener.scala:59)
    at akka.io.TcpListener.<init>(TcpListener.scala:56)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at akka.util.Reflect$.instantiate(Reflect.scala:68)
    at akka.actor.ArgsReflectConstructor.produce(IndirectActorProducer.scala:101)
    at akka.actor.Props.newActor(Props.scala:212)
    at akka.actor.ActorCell.newActor(ActorCell.scala:650)
    at akka.actor.ActorCell.create(ActorCell.scala:676)
    at akka.actor.ActorCell.invokeAll$1(ActorCell.scala:549)
    at akka.actor.ActorCell.systemInvoke(ActorCell.scala:571)
    at akka.dispatch.Mailbox.processAllSystemMessages(Mailbox.scala:293)
    at akka.dispatch.Mailbox.run(Mailbox.scala:228)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:241)
    at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

How can I resolve this issue? I'm wondering why it works with my private IP but not with the public IP.

Also, I accidentally mistyped a private IP address, but the connection still fails.

Does the TCP socket server check if the client is available when trying to bind a port?


Solution

  • When I try to create a TCP connection between these devices using my router's public IP, I'm unable to establish the connection. The connection attempt fails.

    You can't bind to an IP address not assigned to that machine. You need to bind to either the wildcard address or to the machine's LAN address.

    Note that you still may not be able to reach the machine on the router's public IP address from inside the LAN. That requires the router to support hairpin NAT, which not all routers do.