I am trying to experiment with UDP hole punching to try to connect 2 computers. The 2 computers I am trying to connect are on 2 different Wifi created by the same router. Let me call these computers C1 and C2. So I want to create a connection between C1 and C2. I have 2 public server IPs available where I run a UDP listener server. These servers are S1 and S2.
Experiment 1:
- Send a UDP packet from C1 to S1 and print the IP and port of the incoming packet. I get IP_C1 and PORT_C1.
- Send a message back to C1 from S1 using IP_C1 and PORT_C1. This works fine as expected and it remains connected. So in a way I have already punched a hole for S1 to communicate directly with C1.
Experiment 2:
- Send a UDP packet from C1 to S1 and get IP_C1 and PORT_C1.
- Send a UDP packet from C2 to S1 and get IP_C2 and PORT_C2.
- Use S1 to send a message back to C1 using IP_C1 and PORT_C1. This works as before.
- Use S1 to send a message back to C2 using IP_C2 and PORT_C2. This also works fine.
So now both C1 and C2 are accessible from S1. NOTE: What I see is that IP_C1 and IP_C2 are the same so lets call them IP_C. Ports however are different.
Continuing this I tried sending a message from C1 to C2 using IP_C and PORT_C2 as the destination. C2 did not receive the message as expected I then send a message from C2 to C1 using IP_C and PORT_C1. This is where I start getting stuck: I expected that now the message of C2 will reach C1 since C1 already punched a hole in the NAT going to C2. But C1 does not receive the message. Continuing if I now send another message from C1 and C2. C2 does not get it. But even now if I send a message from S1 to C1 or C2 it works.
Experiment 3:
To check whether the IP and port are maintained when the client sends another UDP packet I did the following:
- Send a UDP packet from C1 to S1 and get IP_C1 and PORT_C1.
- Send a UDP packet from C2 to S1 and get IP_C2 and PORT_C2.
- Send a UDP packet from C1 to S2 and get IP_C1_S2 and PORT_C1_S2.
- Send a UDP packet from C2 to S2 and get IP_C2_S2 and PORT_C2_S2.
Like S1, S2 also easily connects and I see IP_C1=IP_C1_S2=IP_C2=IP_C2_S2=IP_C and PORT_C1=PORT_C1_S1 and PORT_C2=PORT_C2_S2.
So the ports are maintained.
Even with this scenario I am not able to establish communication between C1 and C2. I am using luasocket library to send udp packets and do the experiments. If anyone can help guide me as to what other experiments I can do to do this successfully it would be much appreciated.
Thanks