Creating socket between two machines behind NAT

Chaosis

New Member
I have a P2P Java application that acts as both a server and a client. It is given a list of public IP addresses and the application attempts to create socket communication. The list is only comprised of (and can only be comprised of) public IP addresses.I have the application running on two machines that are both behind the same NAT. I have forwarded both ports (one for each machine) so that they are publicly available and any incoming messages to either machines is received correctly (ie. port forwarding is working correctly). However, a problem arises when two local machines try to communicate with each other using the public IP. For example, Machine A has a private IP of 192.168.1.1 and Machine B has a private IP of 192.168.1.2 and they accept on ports 2001 and 2002 respectively. Both ports have been forwarded on the router. They both have the same public IP of 8.8.8.8. Attempts made by A to connect to B using the public IP result in a packet capture that looks as follows:192.168.1.1:4000 -> 8.8.8.8:2002
192.168.1.2:2002 -> 192.168.1.1:4000This results is B's listening socket never accepting. However, if I use the local IP of B instead, everything is fine:192.168.1.1:4000 -> 192.168.1.2:2002
192.168.1.2:2002 -> 192.168.1.1:4000Is there a way to form a socket connection between two machines behind the same NAT using their public IP addresses?
 
Back
Top