Alex Rivera | Logout

TCP Hole Punching

Asked 2012-01-11T12:20:23.577
20

I'm trying to implement TCP hole punching with windows socket using mingw toolchain. I think the process is right but the hole doesn't seems to take. I used this as reference.

  1. A and B connect to the server S
  2. S sends to A, B's router IP + the port it used to connect to S
  3. S does the same for B
  4. A start 2 threads:
    • One thread tries connecting to B's router with the info sent by S
    • The other thread is waiting for an incoming connection on the same port used to connect to its router when it connected to S
  5. B does the same

I have no issue in the code I think since:

  • A and B does get each other ip and port to use
  • They are both listening on the port they used to connect to their router when they contacted the server
  • They are both connecting to the right ip and port but get timed out (code error 10060)

I am missing something ?

EDIT: With the help of process explorer, I see that one of the client managed to establish a connection to the peer. But the peer doesn't seems to consider the connection to be made.

Here is what I captured with Wireshark. For the sake of the example, the server S and the client A are on the same PC. The server S listens on a specific port (8060) redirected to that PC. B still tries to connect on the right IP because it sees that the public address of A sent by S is localhost and therefore u

Edit
Report

1 Answer

0
  1. Use Wireshark to check tcp connection request(3-way Handhsake process) is going properly.

  2. Ensure your Listener thread is having select() to de-multiplex the descriptor.

  3. sockPeerConect(socket used to connect Other peer) is FD_SET() in Listener Thread.

  4. Ensure your are checking

     int Listener Thread()
     {
       while(true)
       {
           FD_SET(sockPeerConn);
           FD_SET(sockServerConn);
           FD_SET(nConnectedSock );
          if (FD_ISSET(sockPeerConect)
          {
            /// and calling accept() in side the
            nConnectedSock = accept( ....);
    
           }
           if (FD_ISSET(sockServerConn)
           {
            /// receive data from Server
            recv(sockServerConn );
    
           }
           if (FD_ISSET(nConnectedSock )
           {
            /// Receive data from Other Peer
             recv(nConnectedSock );
    
           }
    
       }
      }
    

5.Ensure you are simultaneously starting peer connection A to B and B to A.
6.Start your Listener Thread Prior to Connection to server and Peer and have Single Listener Thread for receiving Server and Client.

answered 2012-01-11T17:03:30.043

Your Answer