Copyright: This article is published by SkyMountain at http://blog.csdn.net/skymountain /. The author reserves all copyrights and must indicate the source for reprinting. Otherwise, it is deemed as infringement. Last night, I suddenly came up with a method that could accurately guess the NAT shot port. I am referring to the type of NAT that is sensitive to the target port. Such NAT is hard to penetrate before.
This method uses three new Sockets (that is, the new local port) to detect . Create three new sockets, each of which corresponds to a port that has never been used before. BIND. Then, in chronological order, the first socket is sent to the stun Server The second socket sends a data packet to the target server. , That is, the drilling action is executed, and the third socket is sent to the stun server again Data. All three actions will open a new shooting port on the server. . The interval between the three actions is very short, about Ms. In this way, we have reason to believe that the second socket The allocated ing port is likely to be between the first and second ing ports. That is, port2> port2 <port3 at the same time in port1. The most likely is:
Port2 = (port1 + port3)/2
If port3 <port1, or port1 and por are detected on the serverThe gap between t3 is too large, so it is likely that the port number on NAT has started to score againSo the client should repeat the process. Repeated several timesThe chances of success are much higher.
An exception is that the port maintains NAT, that is, when a NAT port is assigned, Try to make the new port number consistent with the Intranet port number. However, this situation is easy to identify.The server simply compares the client's intranet port and ing port.. If this type of NAT is found, the above formula must be modified:
Port2 = Intranet Port Number of the second socket
Of course, no matter what the method of port speculation is, there is still no 100% connection rate between users in the intranet. But you can guess the port method.To improve the cross-NAT connection rate, thus reducing the burden on the data transfer server.