Search code examples
javadockersocketsnetwork-programmingtcp

Java socket client port forwarding


I'm currently coding a client-server java application. I use the java socket api for sending data from the client to the server and vice-versa. The server is running on windows on port 9001. The client is in a docker container on the same machine. What port do I have to bind in docker so that the client in the docker container can connect to the server?

I've tried binding the server port, which obviously didn't work because it's already taken by the server.


Solution

  • The client needs to connect to the host on port 9001.

    The common way to do that is to add the host-gateway as an extra host by adding --add-host=host.docker.internal:host-gateway as a parameter to your docker run command.

    Then the client can connect to your server program using host.docker.internal as the hostname (and 9001 as the port name).