TCPNAT traversal method and system based on time synchronization

By adopting the combination method of time synchronization and socket option SO_REUSEADDR in TCP NAT travel, the problem of low success rate of TCP NAT travel is solved, and high-precision time synchronization and communication quality improvement is achieved.

CN118158190BActive Publication Date: 2024-11-08SHANGHAI BEIRUI INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410202460.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-02-23
Publication Date
2024-11-08
Estimated Expiration
2044-02-23

AI Technical Summary

Technical Problem

In the prior art, the success rate of TCP NAT traversal is very low, especially in the NAT environment. The firewall may intercept the TCP syn packet of the source client, resulting in the failure of the connection.

Method used

Using the TCP NAT crossing method based on time synchronization, the server time is used as the agreed initiation time, and the source client and the target client perform preset time mapping to ensure that TCP connections are initiated at the same time, and the socket option SO_REUSEADDR is set to avoid being intercepted by NAT devices. , and obtain the heartbeat packet error through the moving average to improve the time synchronization accuracy.

Benefits of technology

It significantly improves the success rate of TCP NAT traversal and P2P communication quality, ensures that the source client and the target client can initiate TCP SYN packets simultaneously in high precision, bypass the firewall of the NAT device, and improves the communication success rate.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118158190B_ABST
    Figure CN118158190B_ABST
Patent Text Reader

Abstract

The present invention provides a TCP NAT traversal method and system based on time synchronization, the method comprising: a source client sends a source client's external address after NAT to a target client through a server; the target client obtains the corresponding server time as the agreed initiation time through a preset time mapping method based on the local initiation time of the target client, the target client sends the target client's external address after NAT and the agreed initiation time to the source client through the server; the source client obtains the corresponding source client's local initiation time through a preset time mapping method based on the agreed initiation time; the source client establishes a TCP connection with the target client's external address after NAT at the local initiation time of the source client, and the target client establishes a TCP connection with the source client's external address after NAT at the local initiation time of the target client. The present invention significantly improves the success rate of TCP NAT traversal, thereby improving the P2P communication success rate and communication quality.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the technical field of computer network communication, and relates to a TCP NAT traversal method based on time synchronization, and in particular to a TCP NAT traversal method and system based on time synchronization. Background Art

[0002] NAT (Network Address Translation) is a network communication technology that is often used to connect private networks to the public Internet. The main function of NAT is to map multiple devices on the internal network to a single or multiple public IP addresses so that multiple devices can share a limited number of public IP addresses. In a network environment using NAT, specific technologies and protocols can be used to allow internal network devices to establish direct communication with the external network without the need for intermediate NAT devices to block or modify data packets. This technology is NAT traversal. NAT traversal can be applied to multiple scenarios such as point-to-point (P2P) communication, real-time communication applications (such as VoIP and video chat), and peer-to-peer networks. In particular, in scenarios such as audio and video and large file transfer, there is often a need for P2P communication between terminal software.

[0003] However, the mainstream implementation of P2P communication is based on UDP traversal. Since UDP is a second-class traffic in the operator, it is often QOSed, which will lead to a series of problems such as slow speed, high packet loss rate, large delay jitter, etc. TCP traversal can avoid QOS, but there are few related studies on TCP traversal. In addition, since many NAT environments have firewalls, the TCP syn packet of the source client has reached the NAT device of the target client while the TCP syn packet of the target client has not been sent out through the NAT device. In this case, the syn packet of the source client will be considered an illegal packet, and its IP and port after NAT will be blacklisted. Even if the TCP protocol retransmits the syn packet, it still cannot traverse the NAT device. Therefore, the success rate of TCP NAT traversal in the existing technology is very low. Summary of the invention

[0004] The object of the present invention is to provide a TCP NAT traversal method and system based on time synchronization, so as to solve the technical problem that the success rate of TCP NAT traversal in the prior art is very low.

[0005] In a first aspect, the present invention provides a TCP NAT traversal method based on time synchronization, comprising:

[0006] The source client establishes a TCP connection with the server through the corresponding source NAT device, records the external address of the source client after NAT, closes the TCP connection between the source client and the server, and the source client sends the external address of the source client after NAT to the target client through the server;

[0007] The target client establishes a TCP connection with the server through the corresponding target NAT device, records the external address of the target client after NAT, closes the TCP connection between the target client and the server, and the target client obtains the corresponding server time as the agreed initiation time through a preset time mapping method based on the local initiation time of the target client. The target client sends the external address of the target client after NAT and the agreed initiation time to the source client through the server;

[0008] The source client obtains the corresponding source client local initiation time through a preset time mapping method based on the agreed initiation time;

[0009] The source client establishes a TCP connection with the external address behind NAT of the target client at the local initiation time of the source client, and the target client establishes a TCP connection with the external address behind NAT of the source client at the local initiation time of the target client;

[0010] The source client and the target client respectively establish a long connection with the server, and the interaction of the external address after NAT between the source client and the target client is realized through the long connection; the ports for the TCP connection between the source client and the target client both set the socket option to SO_REUSEADDR, and the external address after NAT includes a public IP address and a port number.

[0011] In one embodiment of the present invention, the preset client local initiation time is the preset client current time plus the preset exchange time; the preset time mapping method of the preset client is: the agreed initiation time is equal to the sum of the preset client current time and the preset exchange time minus the difference obtained by deducting the heartbeat error;

[0012] Among them, the heartbeat error is the sum of the cumulative error and the clock offset, the cumulative error is the product of the number of heartbeats and the average heartbeat error, the clock offset is the quotient obtained by multiplying the time offset value by the average heartbeat error and dividing it by the heartbeat interval, the time offset value is the time value obtained by adding the preset exchange time to the current time of the preset client minus the last heartbeat time, the heartbeat average error is the moving average of the preset number of heartbeat clock differences between the preset client and the server, the heartbeat clock difference is the time for each preset client to send a heartbeat packet plus half the RTT minus the time for the server to receive the heartbeat packet for the corresponding time, and the preset client is the target client or the source client.

[0013] In an embodiment of the present invention, the preset number of times is 10 times.

[0014] In one embodiment of the present invention, the preset exchange time is set based on the network signal and the user experience, the preset exchange time is inversely proportional to the network signal strength, and the preset exchange time is inversely proportional to the user experience.

[0015] In one embodiment of the present invention, the preset exchange time is 3 seconds.

[0016] In one embodiment of the present invention, the source client obtains the corresponding source client local initiation time by a preset time mapping method based on the agreed initiation time, including:

[0017] The source client obtains the corresponding source client local initiation time through a preset time mapping method based on the agreed initiation time;

[0018] The target client and the source client are both set to sleep for a preset duration.

[0019] In one embodiment of the present invention, the preset duration of the preset client is a time value obtained by adding the server time agreed to initiate the TCP connection to the accumulated error and then subtracting the preset client current time.

[0020] In a second aspect, the present invention further provides a TCP NAT traversal system based on time synchronization, comprising a source client, a target client and a server:

[0021] The server is used to establish a persistent connection with the source client and the target client, and to establish a TCP connection with the source client and the target client, respectively;

[0022] The source client is used to establish a TCP connection with the server through a source NAT device, record the external address of the source client after NAT, close the TCP connection between the source client and the server, send the external address of the source client after NAT to the target client through the server, obtain the corresponding local time of the source client through a preset time mapping method based on the agreed initiation time, and establish a TCP connection with the external address of the target client after NAT at the local initiation time of the source client;

[0023] The target client is used to establish a TCP connection with the server through the target NAT device, record the external address of the target client after NAT, close the TCP connection between the target client and the server, obtain the corresponding server time as the agreed initiation time through a preset time mapping method based on the local initiation time of the target client, send the external address of the target client after NAT and the agreed initiation time to the source client through the server, and establish a TCP connection with the external address of the source client after NAT at the local initiation time of the target client;

[0024] Among them, the interaction of the external address after NAT between the source client and the target client is realized through the long connection; the ports for TCP connection between the source client and the target client both set the socket option to SO_REUSEADDR, and the external address after NAT includes a public IP address and a port number.

[0025] As described above, the TCP NAT traversal method and system based on time synchronization described in the present invention have the following beneficial effects:

[0026] 1. The present invention uses the server time as the agreed initiation time, so that the source client and the target client can realize TCP NAT traversal based on time synchronization, thereby improving the success rate of TCP NAT traversal;

[0027] 2. A preset time mapping method is designed to achieve simultaneous sending of TCP SYN packets by the source client and the target client with high-precision time synchronization, significantly improving the success rate of TCP NAT traversal, thereby improving the success rate and quality of P2P communication;

[0028] 3. The single average error of the heartbeat packet is obtained by means of moving average, which further ensures the accuracy of the preset time mapping. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] Figure 1 A schematic flow chart of a TCP NAT traversal method based on time synchronization according to an embodiment of the present invention is shown.

[0030] Figure 2 A schematic diagram of the structure of a TCP NAT traversal system based on time synchronization according to an embodiment of the present invention is shown. DETAILED DESCRIPTION

[0031] The following describes the embodiments of the present invention by specific examples, and those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict.

[0032] The principles and implementation methods of the TCP NAT traversal method and system based on time synchronization of this embodiment will be described in detail below, so that those skilled in the art can understand the TCP NAT traversal method and system based on time synchronization of this embodiment without creative work.

[0033] RTT, or Round-Trip Time, is a metric for calculating network communication latency. It represents the time it takes from sending a data packet to receiving the corresponding data packet. RTT is often used to measure the response time of a network connection and is an important performance metric, especially in real-time applications and network performance optimization.

[0034] In order to solve the above technical problems existing in the prior art, an embodiment of the present invention provides a TCP NAT traversal method based on time synchronization.

[0035] Figure 1 The schematic diagram of the process of the TCP NAT traversal method based on time synchronization according to an embodiment of the present invention is shown. Figure 1 As shown, the TCP NAT traversal method based on time synchronization of the present invention mainly includes steps S100 to S400.

[0036] Step S100: The source client establishes a TCP connection with the server through the corresponding source NAT device, records the external address of the source client after NAT, closes the TCP connection between the source client and the server, and the source client sends the external address of the source client after NAT to the target client through the server.

[0037] Specifically, the source client uses a random port to establish a TCP connection with the server. The purpose of this is to obtain the source client's public IP address srcIP and port srcNatPort after NAT, that is, to obtain the source client's external address after NAT. For example, if srcIP is 117.158.134.217 and srcNatPort is 5002, then the source client's external address after NAT is 117.158.134.217:5002. After the source client records its public IP address srcIP and port srcNatPort, the TCP connection can be closed. It should be noted that this TCP connection is a short connection, which is used for TCP hole punching, that is, NAT traversal.

[0038] Since there is a persistent connection between the source client and the server, the existing persistent connection control information exchange between the source client and the server can be used to send a data packet containing srcIP:srcNatPort to the server, and then the server forwards the data packet to the target client. The purpose is to notify the target client to prepare to connect to the external address of the source client after NAT, so that the target client can know the public IP address and port of the source client after NAT-A conversion, laying the data foundation for the subsequent TCP connection between the source client and the target client.

[0039] Optionally, in addition to the source client's external address after NAT srcIP:srcNatPort, the data packet may also include an identifier that indicates "ready to connect". For example, the source client and the target client agree that the data packet sent by the source client is in json format, specifically {"srcIP":"117.158.134.217","srcNatPort":"5002","message":"connect"}. After receiving the data packet, the target client will know that it is ready to initiate a connection to the address shown in the data packet. Other data formats can be selected according to actual needs, and it can also be agreed that when the target client receives the data packet containing the source client's external address after NAT, it means that it is ready to initiate a connection, and there is no need to include an identifier that indicates "ready to connect".

[0040] Step S200: The target client establishes a TCP connection with the server through the corresponding target NAT device, and records the target client's external address after NAT, closes the TCP connection between the target client and the server, and the target client obtains the corresponding server time as the agreed initiation time through a preset time mapping method based on the local initiation time of the target client. The target client sends the target client's external address after NAT and the agreed initiation time to the source client through the server.

[0041] Specifically, the target client uses a random port to establish a TCP connection with the server. The purpose of this is to obtain the target client's public IP address dstIP and port dstNatPort after NAT, that is, to obtain the target client's external address after NAT. For example, if dstIP is 117.158.134.210 and dstNatPort is 8001, then the target client's external address after NAT is 117.158.134.210:8001. After the target client records its public IP address dstIP and port dstNatPort, the TCP connection can be closed. It should be noted that this TCP connection is a short connection, which is used for TCP hole punching, that is, NAT traversal.

[0042] At this time, the target client converts the corresponding server time ts based on the time when it wants to initiate the TCP connection (that is, the local initiation time of the target client) as the agreed initiation time between the target client and the source client, and the above conversion needs to be calculated based on the preset time mapping method. Afterwards, the target client uses the existing long connection between the target client and the server to send a data packet containing dstIP:dstNatPort and the agreed initiation time to the server, and then the server forwards the data packet to the source client. The purpose is to notify the source client of two pieces of information, one of which is the external address of the target client after NAT for the source client to connect, and the other is the agreed initiation time, and the agreed initiation time needs to be based on the server time, so that the target client knows the public IP address and port of the target client after NAT-A conversion and the agreed initiation time of the TCP connection. Therefore, the time when the source client and the target client initiate the TCP connection is synchronized.

[0043] Optionally, in addition to the target client's external address after NAT dstIP:dstNatPort and the agreed initiation time, the data packet may also include an identifier that reflects "ready to connect". For example, the target client and the source client agree that the data packet sent by the target client is in json format, specifically {"srcIP":"117.158.134.210","srcNatPort":"8001","co_time":ts,"message":"connect"}. After receiving the data packet, the target client can initiate a connection to the address shown in the data packet on time. Other data formats can be selected according to actual needs, and it can also be agreed that when the source client receives the above data packet, it means that it is ready to initiate a connection, and there is no need to include an identifier that reflects "ready to connect".

[0044] Optionally, the preset client local initiation time is the preset client current time plus the preset exchange time, that is, the target client sends the above data packet containing dstIP:dstNatPort and the agreed initiation time to the source client through the server at its own current time to notify the source client to prepare to initiate a TCP connection, but considering that it takes time for the target client and the source client to exchange information through the server, the actual initiation time is the target client current time plus the preset time. Specifically, the local preset time mapping method of the preset client is: the agreed initiation time is equal to the sum of the preset client current time and the preset exchange time minus the difference obtained by the heartbeat error, refer to the following formula:

[0045] ts=now+punchdelay-dt-ddtma*(now+punchdelay-hbtime / hbInterval)

[0046] Among them, now is the current time of the preset client, ts is the corresponding server time, dt is the cumulative error, ddtma is the average heartbeat error, hbtime is the last heartbeat time of the preset client, and hbInterval is the heartbeat interval. The heartbeat error is the sum of the cumulative error and the clock offset. The cumulative error is the product of the number of heartbeats and the average heartbeat error. The clock offset is the quotient of the time offset value multiplied by the average heartbeat error and divided by the heartbeat interval. The time offset value is the current time of the preset client plus the preset exchange time minus the time of the last heartbeat. The heartbeat average error is the moving average of the preset number of heartbeat clock differences between the preset client and the server. The heartbeat clock difference is the time each time the preset client sends a heartbeat packet plus half the RTT minus the time the server receives the corresponding heartbeat packet. The preset client is the target client or the source client. Based on the local initiation time now of the target client a +punchdelay, substitute it into the formula to calculate the corresponding server time ts as the agreed launch time. After the source client receives the above agreed launch time ts, it can also reversely calculate the source client local launch time now corresponding to the agreed launch time ts according to the above calculation formula b +punchdelay. The reason for setting punchdelay is to reserve time for information exchange.

[0047] It should be noted that the reason for setting the above time mapping method is that there is usually a time error in the interaction between the client and the server. For example, when the client sends a heartbeat packet heartbeat to the server, the time t1 is recorded, and when the server receives the heartbeat packet heartbeat, the time is recorded as t2. Here, since the processing logic on the server side is time-consuming, it is negligible (if it is a distributed server, it is necessary to synchronize the time between the server and the client regularly). At the same time, the server feeds back heartbeatAck to the client, and when the client receives heartbeatAck, the time t3 is recorded. In this process, the one-way sending time from the client to the server is RTT / 2, that is, (t3-t1) / 2, but calculated on the server side, the one-way time from the client to the server is t2-t1, so because the clock speeds of the client and the server are different, in this heartbeat packet interaction process, the client time is faster than the server time by dt, dt is the difference between RTT / 2 and t2-t1, that is, t1+RTT / 2-t2. Afterwards, as the number of heartbeat packet interactions increases, dt gradually accumulates, but although the client and server clocks are different, the speed of a single clock can be considered stable under non-extreme temperature or voltage changes, so dt will grow linearly, for example, the time error between the two is 2ms when the heartbeat packet interacts for the first time, and 4ms for the second time, which is accumulated in this way. The change value ddt of dt is dt minus d(t-1), and ddt is close to a constant in theory, but it is actually affected by network jitter, and it will fluctuate up and down at a constant value. In order to reduce the impact of network fluctuations, the present invention uses a moving average filter to obtain the moving average ddtma of ddt, so that the calculation of the heartbeat average error is more accurate and stable, and the time when the source client and the target client initiate the TCP connection is synchronized with high precision, so as to realize TCP NAT traversal based on high-precision time synchronization, and improve the success rate of TCP NAT traversal.

[0048] Optionally, the preset number of times is 10. That is, the 10-item moving average of DDT is selected as DDTMA. Specifically, the calculation formula is as follows

[0049] ddtma=[ddt+dd(t-1)+dd(t-2)+…+dd(t-9)] / 10

[0050] Wherein, ddt represents the change value of dt at the t-th heartbeat, and similarly dd(t-9) represents the change value of dt at the t-9th heartbeat. Other preset times of numerical values ​​may be selected according to actual conditions.

[0051] Optionally, the current time of the preset client is obtained by converting the current timestamp of the preset client into nanoseconds, and each time used in the above calculation must be accurate to nanosecond level to improve the time accuracy of the source client and the target client initiating the connection at the same time.

[0052] Optionally, the preset exchange time punchdelay is set based on the network signal and user experience. The higher the network signal strength, the faster the information is exchanged, and the shorter the preset exchange time is; the better the user experience, the shorter the preset exchange time needs to be set. Preferably, the preset exchange time is 3 seconds. According to the actual situation, you can choose to set other preset exchange time values.

[0053] Step S300: The source client obtains the corresponding source client local time through a preset time mapping method based on the agreed initiation time.

[0054] Specifically, the calculation method for the source client to obtain the corresponding source client local time based on the agreed initiation time can refer to the above introduction to the preset time mapping method.

[0055] Optionally, step S300 includes: the source client obtains the corresponding source client local initiation time through a preset time mapping method based on the agreed initiation time, and the target client and the source client both set a program sleep of a preset duration. That is, after the source client obtains the corresponding source client local time, the source client and the target client both set a program sleep to match the time consumption of the interactive information.

[0056] Preferably, the program sleep time is set in such a way that the preset time of the client is the time obtained by adding the accumulated error to the server time of the TCP connection initiation agreement and subtracting the preset client current time. Specifically, the source client and the target client calculate their own sleep time respectively. For example, the time of the target client is now a , the cumulative error is dt a , then the sleep time is ts+dt a -now a ; The time of the source client is now b , the cumulative error is dt b , the sleep time is ts+dt b -mow b It should be noted that the sleep time of the above setting is slightly shorter than the preset exchange time punchdelay, but the two values ​​are very close. Take the source client as an example. The local initiation time of the source client is now b +punchdelay, program sleep ts+dt b -now bThe subsequent time is very close to the local initiation time of the source client, and a TCP connection with the target client can be initiated according to the local initiation time of the source client.

[0057] Step S400: the source client establishes a TCP connection with the external address behind NAT of the target client at the local initiation time of the source client, and the target client establishes a TCP connection with the external address behind NAT of the source client at the local initiation time of the target client.

[0058] Specifically, the source client sets SO_REUSEADDR to establish a TCP connection with dstIP:dstNatPort. At the same time, the target client sets SO_REUSEADDR to establish a TCP connection with srcIP:srcNatPort. At this time, both parties send TCP SYN packets almost at the same time, which can avoid being intercepted by the firewall on the NAT device, and significantly improve the success rate of TCP NAT traversal. It should be noted that TCP socket programming is usually divided into two types: actively opening connect and passively opening listen, but initiating a connection at the same time in step S400 is a special usage of the TCP protocol. The present invention applies this special usage to TCP NAT traversal, which significantly improves the success rate of TCP NAT traversal.

[0059] The source client and the target client establish persistent connections with the server respectively, and the interaction of the external address after NAT between the source client and the target client is realized through persistent connections; the ports for TCP connection between the source client and the target client both set the socket option to SO_REUSEADDR, and the external address after NAT includes the public IP address and port number.

[0060] The TCP NAT traversal method based on time synchronization in the embodiment of the present invention uses the server time as the agreed initiation time, so that the source client and the target client can realize TCP NAT traversal based on time synchronization, thereby improving the success rate of TCP NAT traversal. At the same time, a preset time mapping method is designed to ensure the high accuracy of time synchronization. In addition, the single average error of the heartbeat packet is obtained by a moving average method, which further ensures the accuracy of the preset time mapping, significantly improves the success rate of TCP NAT traversal, and thus improves the success rate and communication quality of P2P communication.

[0061] In order to solve the above technical problems existing in the prior art, an embodiment of the present invention further provides a TCP NAT traversal system based on time synchronization.

[0062] Figure 2 FIG. 1 shows a schematic diagram of a TCP NAT traversal system based on time synchronization according to an embodiment of the present invention. Figure 2As shown, the TCP NAT traversal system based on time synchronization according to an embodiment of the present invention includes a source client, a target client and a server.

[0063] The server is used to establish a long connection with the source client and the target client respectively, and to establish a TCP connection with the source client and the target client respectively.

[0064] The source client is used to establish a TCP connection with the server through a source NAT device, record the external address of the source client after NAT, close the TCP connection between the source client and the server, send the external address of the source client after NAT to the target client through the server, obtain the corresponding local time of the source client through a preset time mapping method based on the agreed initiation time, and establish a TCP connection with the external address of the target client after NAT at the local initiation time of the source client.

[0065] The target client is used to establish a TCP connection with the server through the target NAT device, record the external address of the target client after NAT, close the TCP connection between the target client and the server, obtain the corresponding server time as the agreed initiation time through a preset time mapping method based on the local initiation time of the target client, send the external address of the target client after NAT and the agreed initiation time to the source client through the server, and establish a TCP connection with the external address of the source client after NAT at the local initiation time of the target client.

[0066] The interaction of the external address after NAT between the source client and the target client is realized through a persistent connection; the ports for TCP connection between the source client and the target client are both set with the socket option SO_REUSEADDR, and the external address after NAT includes the public IP address and port number.

[0067] The TCP NAT traversal system based on time synchronization in the embodiment of the present invention uses the server time as the agreed initiation time, so that the source client and the target client can realize TCP NAT traversal based on time synchronization, thereby improving the success rate of TCP NAT traversal. At the same time, a preset time mapping method is designed to ensure the high precision of time synchronization. In addition, the single average error of the heartbeat packet is obtained by a moving average method, which further ensures the accuracy of the preset time mapping, significantly improves the success rate of TCPNAT traversal, and thus improves the success rate and communication quality of P2P communication. .

[0068] Although the embodiments disclosed in the present invention are as above, the contents described are only embodiments adopted for facilitating the understanding of the present invention and are not intended to limit the present invention. Any technician in the technical field to which the present invention belongs can make any modifications and changes in the form and details of the implementation without departing from the spirit and scope disclosed in the present invention, but the protection scope of the present invention shall still be subject to the scope defined in the attached claims.

Claims

1. A TCP NAT traversal method based on time synchronization, comprising: The source client establishes a TCP connection with the server through the corresponding source NAT device, records the external address of the source client after NAT, closes the TCP connection between the source client and the server, and the source client sends the external address of the source client after NAT to the target client through the server; The target client establishes a TCP connection with the server through the corresponding target NAT device, records the external address of the target client after NAT, closes the TCP connection between the target client and the server, and the target client obtains the corresponding server time as the agreed initiation time through a preset time mapping method based on the local initiation time of the target client. The target client sends the external address of the target client after NAT and the agreed initiation time to the source client through the server; The source client obtains the corresponding source client local initiation time through a preset time mapping method based on the agreed initiation time; The source client establishes a TCP connection with the external address behind NAT of the target client at the local initiation time of the source client, and the target client establishes a TCP connection with the external address behind NAT of the source client at the local initiation time of the target client; The source client and the target client respectively establish a persistent connection with the server, and the interaction of the external address after NAT between the source client and the target client is realized through the persistent connection; the ports for the TCP connection between the source client and the target client are both set to SO_REUSEADDR as the socket option, and the external address after NAT includes a public IP address and a port number; The preset client local initiation time is the preset client current time plus the preset exchange time; The default time mapping method of the default client is: The agreed initiation time is equal to the sum of the preset client current time and the preset exchange time minus the difference between the heartbeat error and the time; Among them, the heartbeat error is the sum of the cumulative error and the clock offset, the cumulative error is the product of the number of heartbeats and the average heartbeat error, the clock offset is the quotient obtained by multiplying the time offset value by the average heartbeat error and dividing it by the heartbeat interval, the time offset value is the time value obtained by adding the preset exchange time to the current time of the preset client minus the last heartbeat time, the heartbeat average error is the moving average of the preset number of heartbeat clock differences between the preset client and the server, the heartbeat clock difference is the time for each preset client to send a heartbeat packet plus half the RTT minus the time for the server to receive the heartbeat packet for the corresponding time, and the preset client is the target client or the source client.

2. The method according to claim 1, characterized in that The preset number of times is 10 times.

3. The method according to claim 1, characterized in that: The preset exchange time is set based on the network signal and the user experience. The preset exchange time is inversely proportional to the network signal strength. The preset exchange time is inversely proportional to the user experience.

4. The method according to claim 3, characterized in that The preset exchange time is 3 seconds.

5. The method according to any one of claims 1 to 4, characterized in that: The source client acquiring the corresponding source client local initiation time by a preset time mapping method based on the agreed initiation time includes: The source client obtains the corresponding source client local initiation time through a preset time mapping method based on the agreed initiation time; The target client and the source client are both set to sleep for a preset duration.

6. The method according to claim 5, characterized in that The preset duration of the preset client is the time value obtained by adding the server time agreed to initiate the TCP connection to the accumulated error and then subtracting the preset client current time.

7. A TCP NAT traversal system based on time synchronization, comprising a source client, a target client and a server: The server is used to establish a persistent connection with the source client and the target client, and to establish a TCP connection with the source client and the target client, respectively; The source client is used to establish a TCP connection with the server through a source NAT device, record the external address of the source client after NAT, close the TCP connection between the source client and the server, send the external address of the source client after NAT to the target client through the server, obtain the corresponding local time of the source client through a preset time mapping method based on the agreed initiation time, and establish a TCP connection with the external address of the target client after NAT at the local initiation time of the source client; The target client is used to establish a TCP connection with the server through the target NAT device, record the external address of the target client after NAT, close the TCP connection between the target client and the server, obtain the corresponding server time as the agreed initiation time through a preset time mapping method based on the local initiation time of the target client, send the external address of the target client after NAT and the agreed initiation time to the source client through the server, and establish a TCP connection with the external address of the source client after NAT at the local initiation time of the target client; The interaction of the external address after NAT between the source client and the target client is realized through the long connection; the ports for TCP connection between the source client and the target client are both set to SO_REUSEADDR as the socket option, and the external address after NAT includes the public IP address and the port number; The preset client local initiation time is the preset client current time plus the preset exchange time; The default time mapping method of the default client is: The agreed initiation time is equal to the sum of the preset client current time and the preset exchange time minus the difference between the heartbeat error and the time; Among them, the heartbeat error is the sum of the cumulative error and the clock offset, the cumulative error is the product of the number of heartbeats and the average heartbeat error, the clock offset is the quotient obtained by multiplying the time offset value by the average heartbeat error and dividing it by the heartbeat interval, the time offset value is the time value obtained by adding the preset exchange time to the current time of the preset client minus the last heartbeat time, the heartbeat average error is the moving average of the preset number of heartbeat clock differences between the preset client and the server, the heartbeat clock difference is the time for each preset client to send a heartbeat packet plus half the RTT minus the time for the server to receive the heartbeat packet for the corresponding time, and the preset client is the target client or the source client.