the_prole
the_prole

Reputation: 8985

Can I port forward from one interface port to another interface port?

I am "programming TCP sockets" and have some fundamental questions.

When I specify an IP address and port for a TCP client, I know that the IP address belongs to a network interface and not the actual computer as a whole, and that the computer has multiple network interfaces, including the inward facing local host loop back interface.

Does the port specified to the TCP client belong to the specified interface? Like for example, can there be two ports of the same number but on two different interfaces? Like port 5000 of interface A and port 5000 of interface loop back?

I ask this, because I am trying to create a TCP client which will connect via socket to a TCP server running on an Android Virtual Device. The TCP client runs on machine A and the android emulator runs on machine B.

I have looked up port forwarding for Android Emulators, and have learned that the emulator runs behind a virtual router, and that I must configure port forwarding such that TCP socket connections can be routed from one port to another, but these documents only have instructions on how on how to port forward from the loop back interface (the so called local host), so obviously, if I try to create a socket to an interface which is not the loop back interface, I don't get my TCP socket forwarded to the emulator, and my TCP connection fails.

So I wonder if port 5000 on the loop back interface has cousin port 5000 on an outwards facing networking interface?

And I wonder if at some low level I can port forward from the outward facing interface port 5000 to the loopback interface port 5000 which will then forward to port the emulator is situated on.

Is this possible?

Upvotes: 0

Views: 2678

Answers (2)

Admin
Admin

Reputation: 17

U can use port forward software

For example: On Windows

stcppipe.exe localhost port port

On Mac

ipfw add fwd localhost,port tcp from any to any port via en0

On Linux

iptables -t nat -A PREROUTING -p tcp -m tcp --dport21521 -j DNAT --to-destination192.168.0.211:1521

iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -d 192.168.0.211 -p tcp -m tcp --dport 1521 -j SNAT --to-source 192.168.0.132

Upvotes: 1

the_prole
the_prole

Reputation: 8985

adb -s emulator-5554 forward tcp:5000 tcp:6000
echo "1" > /proc/sys/net/ipv4/ip_forward
sudo sysctl -w net.ipv4.conf.enp0s31f6.route_localnet=1
sudo iptables -t nat -A PREROUTING -p tcp --dport 5001 -j DNAT --to 127.0.0.1:5000
sudo iptables -A FORWARD -p tcp -d 127.0.0.1 --dport 5000 -j ACCEPT

Upvotes: 0

Related Questions