I finished a P2P program in Java that is working perfectly in LAN range, and now I'm stuck at the well-known NAT traversal problem. I want to allow my users to connect to each other passing through the NAT boundaries (I know that is impossible to have a 100% about it). I've read a lot about the subject and, although I've understood the concept, I still have doubts doing a TCP hole puching.
What I want to do is to connect two clients to each other (they live under the same NAT), which ips are, respectively, 192.168.100.1 and 192.168.100.2 (they can connect to each other directly). They first connect to a PHP server through sockets at the port 80. The server register its public ips and ports, something like that:
Client 1: 200.000.000.1:1478
Client 2: 120.000.000.1:2547
And the Java sockets of each client returns, upon connection, for instance:
Client 1: Socket[addr=/xx.xx.xx.xx,port=80,localport=54632]
Client 2: Socket[addr=/xx.xx.xx.xx,port=80,localport=41789]
Id est, beyond two tcp connections open with the server through port 80, I also have the private ip, the public ip, the local port and the public port of each client. Then, that's my problem. From now on I don't know how to proceed. What I read was not very clear to me about this step.
Is necessary to have the two clients listening on public ports (1478-2547) or local ports (54632-41789)? Simultaneously making them try to connect on public or private of the other? How many other sockets I need to open? I've done some tests, but I think something is wrong (*connection refused*).
Any help will be welcome.