简体   繁体   中英

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.

Connect to the server's IP:9001. What won't work is if you try to connect to localhost, as from inside the docker container it would look like connecting to the same container.

Of course your server process needs to listen to IP:9001, and listening to localhost:9001 is not sufficient.

Regarding binding: The server opens a port for listening, and that is called binding. The client opens a port for sending (but this sending port address is taken from the ephemeral port range and thus irrelevant) and uses that port to connect to the server port.

See also:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM