TCP Hole Punching

后端 未结 4 934
一生所求
一生所求 2020-12-12 23:14

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

相关标签:
4条回答
  • 2020-12-12 23:23

    A simple solution to traverse into NAT routers is to make your traffic follow a protocol that your NAT already has an algorithm for forwarding, such as FTP.

    0 讨论(0)
  • 2020-12-12 23:40
    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.

    0 讨论(0)
  • 2020-12-12 23:40

    not every router supports tcp hole punching, please check out the following paper which explains in detail:

    Peer-to-Peer Communication Across Network Address Translators

    0 讨论(0)
  • 2020-12-12 23:44

    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

    You can't do this with two threads, since it's just one operation. Every TCP connection that is making an outbound connection is also waiting for an incoming connection. You simply call 'connect', and you are both sending outbound SYNs to make a connection and waiting for inbound SYNs to make a connection.

    You may, however, need to close your connection to the server. Your platform likely doesn't permit you to make a TCP connection from a port when you already have an established connection from that same port. So just as you start TCP hole punching, close the connection to the server. Bind a new TCP socket to that same port, and call connect.

    0 讨论(0)
提交回复
热议问题