A communication method, an electronic device, and a communication system
By obtaining the number of local area network devices and port information of the target device, the problem of low penetration success rate of a large number of devices' private networks is solved, and a more efficient connection process is achieved.
Patent Information
- Application Number
- CN202111596069.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-24
- Publication Date
- 2025-07-29
- Estimated Expiration
- 2041-12-24
AI Technical Summary
The prior art has low success rate in private network penetration scenarios of a large number of devices, resulting in too long connection attempts.
By receiving the connection request from the terminal device, obtain the number of devices in the target device's local area network, determine the port information, including the port gain value or range, and send a response carrying the port information so that the terminal device can attempt to connect.
It improves the success rate of penetration of a large number of devices, reduces the retry time after the connection fails, and increases the probability of successful connection.
Smart Images

Figure CN116346767B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of computer networks and private network penetration. Specifically, it relates to a communication method, an electronic device, and a communication system. Background Art
[0002] Network Address Translation (NAT) refers to the technology that realizes the mutual conversion between the IP addresses of the internal topology network and the public network addresses. This technology converts a large number of internal network IP addresses into one or a small number of public network IP addresses, reducing the occupation of public network IP addresses.
[0003] Private Network Traversal, also known as internal network penetration, NAT penetration, or NAT traversal. Performing NAT penetration is to ensure that data packets with a specific source IP address and source port number are not blocked by NAT devices and are correctly routed to the internal network host. That is to say, since the NAT technology hides the real IP address of the internal network host, host devices in different networks need to perform NAT penetration before communication.
[0004] Currently, most private network penetration scenarios are designed for scenarios where there are a small number of devices in the private network. In the specific practice process, it is found that in scenarios where there are a large number of devices in the private network that require NAT penetration, such as: a large number of video playback devices need to connect to a large number of monitoring camera devices in another internal network. Since the video playback devices and the monitoring camera devices are in different private networks and need to connect through the public network, the video playback devices need to connect to the monitoring camera devices after private network penetration. In these scenarios, when a large number of devices need to perform NAT penetration, it takes a long time to try to penetrate successfully. It can be seen that the current private network penetration method has a low success rate for private network penetration of a large number of devices. Summary of the Invention
[0005] The purpose of the embodiments of this application is to provide a communication method, an electronic device, and a communication system, which are used to improve the problem of low success rate of private network penetration for a large number of devices.
[0006] In a first aspect, the embodiments of this application provide a communication method, including: receiving a first connection request sent by a terminal device for a target device, where the first connection request is used to connect to a target device located in another local area network; obtaining the number of devices in the local area network where the target device is located, and determining the port information of the target device according to the number of devices, where the port information includes a port gain value or a port range, and the port information is used to determine the port range of the target device; sending a first connection response to the terminal device, where the first connection response carries the port information of the target device.
[0007] In the above implementation process, after receiving the first connection request for the target device sent by the terminal device, the number of devices in the local area network where the target device is located is obtained, and the port information of the target device is determined according to the number of devices. The port information includes a port gain value or a port range. By sending a first connection response carrying the port information of the target device to the terminal device, the terminal device can attempt to connect to the target device according to the port information of the target device, thereby improving the success rate of private network penetration for a large number of devices. That is, when there are a large number of devices in the network corresponding to the target device, the probability of successful connection between the terminal device and the target device is increased.
[0008] In one implementation manner of the first aspect, the first connection request carries the unique identification number of the target device; obtaining the number of devices in the local area network where the target device is located includes: determining the public network IP of the local area network where the target device is located according to the unique identification number of the target device; obtaining the number of devices corresponding to the public network IP.
[0009] In the above implementation process, by determining the public network IP of the local area network where the target device is located according to the unique identification number of the target device and obtaining the number of devices corresponding to the public network IP, when there are a large number of devices in the network corresponding to the target device, the probability of successful connection between the terminal device and the target device is increased.
[0010] In one implementation manner of the first aspect, the method further includes: counting the number of unique identification numbers corresponding to each public network IP according to the public network IP and the unique identification number of the device carried in the received heartbeat packet, and determining the number of unique identification numbers corresponding to the public network IP as the number of devices corresponding to each public network IP.
[0011] In the above implementation process, by counting the number of unique identification numbers corresponding to each public network IP and determining the number of unique identification numbers corresponding to the public network IP as the number of devices corresponding to each public network IP, the problem of being unable to obtain the number of devices corresponding to the public network IP is avoided, thereby increasing the probability of successful connection between the terminal device and the target device when there are a large number of devices in the network corresponding to the target device.
[0012] In one implementation manner of the first aspect, it further includes: receiving a reconnection request for the target device sent by the terminal device; adjusting the port information to obtain new port information, where the new port information includes a new port gain value or a new port range; sending a reconnection response to the terminal device, where the reconnection response carries the new port gain value and the first port number most recently used by the target device, or the reconnection response carries the new port range.
[0013] In the above implementation process, by adjusting the port information, new port information is obtained, and a reconnection response including the new port information is sent to the terminal device, thereby avoiding the problem that the terminal device gives up after a connection failure. The terminal device tries to connect again according to the new port information, which can effectively increase the probability of successful connection between the terminal device and the target device.
[0014] In one implementation manner of the first aspect, the new port gain value is determined according to the number of retry attempts for connecting to the target device; wherein, the more the number of retry attempts, the larger the new port gain value; or, the new port range is determined according to the number of retry attempts for connecting to the target device; wherein, the more the number of retry attempts, the larger the new port range. That is to say, the new gain value is related to the number of retry attempts for connecting the terminal device to the target device.
[0015] In one implementation manner of the first aspect, adjusting the port information to obtain new port information includes: obtaining the number of retry attempts for connecting the terminal device to the target device, and using the product of the port gain value and the number of retry attempts as the new port gain value; or, obtaining the number of retry attempts for connecting the terminal device to the target device and the adjustment coefficient corresponding to the current network state, and using the product of the port gain value, the number of retry attempts and the adjustment coefficient as the new port gain value.
[0016] In the above implementation process, the port gain value of the port is further amplified by the adjustment coefficient, thereby expanding the port range of the target device, which is beneficial to further increasing the probability of successful connection to the target device and improving the success rate of private network penetration.
[0017] In one implementation manner of the first aspect, the values of the adjustment coefficients corresponding to different network states are different; when the current network state meets the set requirements, the adjustment coefficient is used to amplify the port gain value; or, when the current network state does not meet the set requirements, the adjustment coefficient is used to reduce the port gain value; wherein the current network state is determined according to the distribution of the port numbers used by the terminal device.
[0018] In a second aspect, an embodiment of the present application further provides a communication method, which is applied to a terminal device and includes: sending a first connection request for connecting to a target device to a server, where the first connection request is used to connect to a target device located in another local area network; receiving a first connection response sent by the server, where the first connection response carries the port information of the target device, and the port information includes a port gain value or a port range; and attempting to connect to the target device according to the port information.
[0019] In the above implementation process, by receiving the first connection response carrying the port information of the target device sent by the server and attempting to connect to the target device according to the port information of the port gain value or port range, the success rate of private network penetration for a large number of devices is improved. That is, when there are a large number of devices in the network corresponding to the target device, the probability of successful connection between the terminal device and the target device is increased.
[0020] In an implementation manner of the second aspect, the port information includes a port gain value, and the first connection response also carries the second port number most recently used by the target device; attempting to connect to the target device according to the port information includes: determining the upper limit value of the port range of the target device as the sum value of the second port number of the target device and the port gain value, and determining the lower limit value of the port range of the target device as the difference value between the second port number of the target device and the port gain value; determining the port range of the target device according to the upper limit value and the lower limit value of the port range; and attempting to connect to the target device according to the port range of the target device.
[0021] In an implementation manner of the second aspect, attempting to connect to the target device according to the port range includes: generating a plurality of second connection requests according to the public IP of the target device and a plurality of port numbers in the port range; and sending the plurality of second connection requests to the target device.
[0022] In the above implementation process, by generating a plurality of second connection requests according to the public IP of the target device and a plurality of port numbers in the port range, the problem of slow connection efficiency when only one connection request is sent each time is avoided. When there are a large number of devices in the network corresponding to the target device, the probability of successful connection between the terminal device and the target device is increased.
[0023] In an implementation manner of the second aspect, it further includes: if a second connection response returned by the target device is not received within a predetermined time period, sending a reconnection request for the target device to the server; receiving the reconnection response sent by the server, and attempting to reconnect to the target device according to the reconnection response; wherein the reconnection response carries a new port gain value and the first port number most recently used by the target device, or the reconnection response carries a new port range.
[0024] In the above implementation process, by the terminal device receiving the reconnection response sent by the server and attempting to reconnect to the target device according to the reconnection response, the problem that the terminal device gives up after a connection failure is avoided. The terminal device attempts to reconnect according to the new port information, which can effectively increase the probability of successful connection between the terminal device and the target device.
[0025] In a third aspect, an embodiment of the present application further provides a communication system, including: a server, a target device, and a terminal device; the server communicates with the target device and the terminal device respectively; the server includes a first processor and a first memory, and the first memory stores machine-readable instructions executable by the first processor. When the machine-readable instructions executable by the first processor are executed by the first processor, the method described in the above first aspect or any possible implementation manner of the first aspect is executed; the terminal device includes a second processor and a second memory, and the second memory stores machine-readable instructions executable by the second processor. When the machine-readable instructions executable by the second processor are executed by the second processor, the method described in the above second aspect or any possible implementation manner of the second aspect is executed.
[0026] In a fourth aspect, an embodiment of the present application further provides a computer program product, including computer program instructions. When the computer program instructions are read and run by a processor, the method described in the above first aspect or any possible implementation manner of the first aspect is executed, or the method described in the above second aspect or any possible implementation manner of the second aspect is executed.
[0027] In a fifth aspect, an embodiment of the present application further provides an electronic device, including: a processor and a memory, and the memory stores machine-readable instructions executable by the processor. When the machine-readable instructions are executed by the processor, the method described in the above first aspect or any possible implementation manner of the first aspect is executed; or when the machine-readable instructions are executed by the processor, the method described in the above second aspect or any possible implementation manner of the second aspect is executed.
[0028] In a sixth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is run by a processor, the method described in the above first aspect or any possible implementation manner of the first aspect is executed; or when the computer program is run by the processor, the method described in the above second aspect or any possible implementation manner of the second aspect is executed. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings required to be used in the embodiments of the present application. It should be understood that the following drawings only show some embodiments of the embodiments of the present application, and therefore should not be regarded as limiting the scope. For those of ordinary skill in the art, other related drawings can be obtained based on these drawings without creative efforts.
[0030] Figure 1 A schematic diagram of the network structure of the applicable application scenario provided by the embodiment of the present application is shown;
[0031] Figure 2 Schematic flowchart of the communication method provided by the embodiment of the present application shown;
[0032] Figure 3 Schematic diagram of the interaction process between the server and the terminal device provided by the embodiment of the present application shown. Specific embodiments
[0033] Next, the technical solutions in the embodiments of the present application will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Usually, the components of the embodiments of the present application described and shown in the accompanying drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the embodiments of the present application to be protected, but only represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative efforts belong to the scope of protection of the embodiments of the present application.
[0034] It should be understood that "first" and "second" in the embodiments of the present application are used to distinguish similar objects. Those skilled in the art can understand that words such as "first" and "second" do not limit the quantity and execution order, and words such as "first" and "second" do not necessarily limit being different.
[0035] Please refer to Figure 1 Schematic diagram of the network structure applicable to the application scenario provided by the embodiment of the present application shown; The network structure of the application scenario applicable to this communication method is introduced below. The network structure here includes: a server, a terminal device, and a target device; The server can communicate with the terminal device and the target device through the Internet via the Hyper Text Transfer Protocol (HTTP) or the Hyper Text Transfer Protocol Secure (HTTPS). When the terminal device and the target device access the Internet, they need to communicate with the server through the exit router (such as the first exit router and the second exit router in the figure) via the Internet. The above server can be understood as an intermediary server, which can refer to a server running on the public network or a cloud host providing Elastic Compute Service (ECS), etc.
[0036] It can be understood that when the above terminal device accesses the Internet, it needs to go through the first egress router, and the first egress router will perform NAT operations on the packets sent by the terminal device (that is, convert the internal network address and port number into a public network address and port number), and communicate with the server, the target device or other devices with the data packets after the NAT operation. Similarly, when the above target device or other devices access the Internet, they need to go through the second egress router, and the second egress router will perform NAT operations on the packets sent by the target device or other devices (that is, convert the internal network address and port number into a public network address and port number), and communicate with the server or the terminal device with the data packets after the NAT operation.
[0037] The terminal device in the embodiment of the present application may be a personal computer, a tablet computer, a mobile phone, an in-vehicle computer, or a mobile Internet device, etc.
[0038] Please refer to Figure 2 the schematic flow chart of the communication method provided by the embodiment of the present application shown; the implementation manner of the communication method may include:
[0039] Step S110: The server receives a first connection request sent by the terminal device, and the first connection request is used to connect to a target device located in another local area network.
[0040] The first connection request refers to a connection request sent by the terminal device to the server when it needs to connect to the target device. Among them, the first connection request may carry the device identifier of the terminal device and the unique identification number of the target device. Among them, the unique identification number of the target device may be information such as the serial number of the target device, the Media Access Control Address (MAC), etc. that can uniquely identify the target device.
[0041] In some embodiments of the present application, the target device and the terminal device are in different local area networks. Usually, the egress router of the target device and the egress router of the terminal device are different egress routers. Therefore, the public IP of the target device (that is, the IP address of the egress router of the target device) is different from the public IP of the terminal device (that is, the IP address of the egress router of the terminal device). Of course, in some scenarios, the egress router of the target device and the egress router of the terminal device are the same egress router, that is, the same egress router has two different public IP addresses. However, the egress router forwards the data packets passed by the target device and the data packets of the terminal device using different public IP addresses respectively. At this time, the public IP of the target device is still different from the public IP of the terminal device. For the sake of easy understanding and explanation, the following takes the case where the egress router of the target device and the egress router of the terminal device are different egress routers as an example for detailed description.
[0042] Step S120: The server obtains the number of devices in the local area network where the target device is located, and determines the port information of the target device according to the number of devices. The port information includes a port gain value or a port range.
[0043] Among them, the number of devices can be used as the port gain value, or the product of the number of devices and a set coefficient can be used as the port gain value, or the sum of the number of devices and a set value can be used as the port gain value. The set coefficient and the set value here can be determined according to experience or the current network environment.
[0044] If the port information includes a port range, then S120 can specifically include that the server determines the port gain value of the target device according to the number of devices, and determines the port range of the target device according to the latest used port number of the target device and the port gain value.
[0045] Determining the port range of the target device according to the latest used port number and the port gain value of the target device specifically includes: determining the upper limit value of the port range of the target device as the sum value of the second port number of the target device and the port gain value, and determining the lower limit value of the port range of the target device as the difference value between the second port number of the target device and the port gain value, and determining the port range of the target device according to the upper limit value and the lower limit value of the port range.
[0046] Step S130: The server sends a first connection response to the terminal device, and the first connection response carries the port information of the target device.
[0047] Next, steps S120 and S130 above will be described together; the server in the embodiment of the present application can collect the heartbeat information (also known as a keep-alive packet or a hello packet) of each device at any time. The heartbeat information here can include: device identifier, public network IP address, and port number (Port), etc. After the server receives the heartbeat information, it can parse the heartbeat information and store the obtained device identifier, public network IP address, and port number in the database. This database can accelerate the process of counting the number of devices. The heartbeat information here not only has the function of keeping alive (that is, telling the server that it is in a live state), but also the device identifier in the heartbeat information can be statistically used by the server, so as to obtain the number of devices in the local area network environment where the terminal device corresponding to the device identifier is located. This number of devices is the basis for calculating the gain value below. Since the server can collect the heartbeat information of each device at any time, and use the number of device identifiers with the same public network IP address parsed from the heartbeat information as the number of devices, that is, the number of all devices sending heartbeat information in the local area network where the target device is located; therefore, the server can count the number of devices corresponding to the public network IP of the target device.
[0048] In some embodiments of the above step S120, the server may perform statistics based on the unique identification number of the target device parsed from the first connection request. This embodiment includes: the server parses the unique identification number of the target device from the first connection request, determines the public IP of the local area network where the target device is located according to the unique identification number of the target device, and then queries and counts the number of devices corresponding to the public IP of the target device in the database. The database records the public IP and the unique identification number of the device carried in the received heartbeat packet. Finally, the number of unique identification numbers corresponding to the public IP is determined as the number of devices corresponding to each public IP.
[0049] In some embodiments of the above step S120, the public IP of the target device may also be carried in the above first connection request. Then the server may parse the public IP of the target device from the first connection request, and then query the number of devices that have sent heartbeat information under this public IP. Since the server previously stores the device identifier, public IP address, and port number in the heartbeat information in the database, the number of data records with the same public IP as the target device and different device identifiers can be queried and counted in the database, and the number of data records is determined as the number of devices corresponding to the public IP of the target device. The database includes the public IP and the unique identification number of the device carried in the received heartbeat packet above. It can be understood that the public IP in the above database may be parsed from the previously received heartbeat information. Of course, in the specific practice process, it may also be that the server has previously counted the number of data records with the same public IP as the target device and different device identifiers in the database, and when needed, directly obtain the number of data records from the memory.
[0050] In some embodiments, the first connection response sent by the server carries the port range of the target device. After receiving the first connection response, the terminal device attempts to penetrate within the port range according to the parsed port range of the target device (i.e., attempts to connect to the target device). In this embodiment, there is no need for the target device to calculate the port range, which helps to reduce the processing pressure on the terminal device.
[0051] In some other embodiments, the first connection response sent by the server carries the port gain value of the target device. After receiving the first connection response, the terminal device calculates the port range based on the parsed port gain value of the target device and the second port number last used by the target device, and attempts to penetrate within the port range (i.e., attempts to connect to the target device). In this embodiment, there is no need for the server to calculate the port range, which helps to reduce the processing pressure on the server. It can be understood that the terminal device can obtain the second port number last used by the target device in various ways: for example, the first connection response not only carries the port gain value but also carries the second port number, and the terminal device can obtain both the port gain value and the second port number by parsing the first connection response; or, the terminal device receives the second port number sent by the server to the terminal device in other ways.
[0052] The above-mentioned second port number last used by the target device includes: the second port number used by the target device when sending the heartbeat packet to the server for the last time, or the second port number used by the target device when sending the connection request packet or other service data packets to the server for the last time.
[0053] In the above implementation process, after receiving the first connection request sent by the terminal device for the target device, obtain the number of devices in the local area network where the target device is located, and determine the port information of the target device according to the number of devices. The port information includes the port gain value or the port range. By sending the first connection response carrying the port information of the target device to the terminal device, the terminal device can be enabled to attempt to connect to the target device according to the port information of the target device, thereby improving the success rate of private network penetration for a large number of devices, that is, when there are a large number of devices in the network corresponding to the target device, the probability of successful connection between the terminal device and the target device is increased.
[0054] The following takes the interaction process between the server and the terminal device as an example for illustration. As Figure 3 shown, the above communication method may include:
[0055] Step S210: The terminal device sends a first connection request to connect to the target device to the server. The first connection request is used to connect to the target device located in another local area network.
[0056] An implementation manner of the above step S210 is, for example: This step is similar to the above step S110. Before the server receives the first connection request, the server can also collect the heartbeat information of each device (including the terminal device, the target device, and other devices). The heartbeat information here may include: device identifier, public network IP address, and port number (Port), etc. Then, the terminal device sends a first connection request to connect to the target device to the server through the HTTP protocol or the HTTPS protocol, so that the server returns a first connection response.
[0057] Step S220: The server receives the first connection request sent by the terminal device, determines the number of devices corresponding to the public IP of the target device according to the first connection request, and then determines the port information of the target device according to the number of devices. The port information includes a port gain value or a port range.
[0058] Step S230: The server sends a first connection response to the terminal device, and the first connection response carries the port information of the target device.
[0059] Among them, the implementation principles and implementation manners of steps S220 to S230 are similar to those of steps S110 to S130. Therefore, the implementation principles and implementation manners are not described here again. If there are unclear points, reference can be made to the description of steps S110 to S130.
[0060] Step S240: The terminal device receives the first connection response sent by the server, determines the port range of the target device according to the port information carried in the first connection response, and attempts to connect to the target device according to the port range.
[0061] In some examples, the first connection response carries the port number most recently used by the target device and the port gain value of the target device. The terminal device parses the first connection response to determine the port number most recently used by the target device and the port gain value of the target device, and then determines the port range of the target device according to the port number and the port gain value. The specific method for determining the port range can refer to the relevant content of the above embodiments and will not be elaborated here.
[0062] In some other examples, the first connection response carries the port range of the target device. The terminal device parses the first connection response to determine the port range of the target device.
[0063] Optionally, the terminal device can generate multiple second connection requests at one time for multiple port numbers within the public IP and port range of the target device, or can generate multiple second connection requests sequentially. The terminal device sends all the multiple second connection requests to the target device at one time, or the terminal device sends multiple second connection requests to the target device sequentially. The multiple connection requests are used for the terminal device to penetrate the private network of the target device, that is, to attempt to connect to the target device through the egress router of the target device.
[0064] It can be understood that after the above terminal device performs private network penetration within the port range in the first connection response, if all ports within the port range fail the attempt, the terminal device can also request new port information from the server again and perform private network penetration according to the new port information. This implementation manner can include: S310 - S360.
[0065] Step S310: The terminal device determines whether the private network penetration of the target device is successful.
[0066] For example, the implementation manner of the above step S310 is as follows: The terminal device can determine whether the private network penetration is successful according to whether a connection response feedback from the target device is received within a predetermined time length. If a connection response feedback from the target device is received within the predetermined time length, it is considered that the private network penetration is successful this time; if a connection response feedback from the target device is not received within the predetermined time length, it is considered that the private network penetration fails this time.
[0067] Step S320: If the private network penetration of the terminal device fails, the terminal device sends a reconnection request for the target device to the server.
[0068] For example, the implementation manner of the above step S320 is as follows: If the private network penetration of the terminal device fails (for example, a second connection response from the target device is not received within a predetermined time length), the terminal device sends a reconnection request for the target device to the server.
[0069] Step S330: The server receives the reconnection request sent by the terminal device, adjusts the port information of the target device, and obtains new port information of the target device. The new port information includes a new port gain value or a new port range.
[0070] In some embodiments, after receiving the reconnection request, the server can increase the port gain value of the target device to obtain a new port gain value. The server can carry the new port gain value in the reconnection response and send it to the terminal device. Here, the new port gain value is related to the retry times of the connection between the terminal device and the target device, that is, the new port gain value is determined according to the retry times of the target device connection. Optionally, the larger the retry times, the larger the new port gain value. For example, the retry times and the new port gain value are positively correlated. For example, the product of the port gain value corresponding to the last failed penetration attempt of the terminal device and the retry times can be used as the new port gain value, that is: port gain value × retry times = new port gain value.
[0071] Optionally, the new port gain value is not only related to the retry times of the connection between the terminal device and the target device, but also related to the adjustment coefficient corresponding to the current network state. The larger the adjustment coefficient, the larger the new port gain value. For example, the product of the port gain value corresponding to the last failed penetration attempt of the terminal device, the retry times, and the adjustment coefficient can be used as the new port gain value, that is: port gain value × retry times × adjustment coefficient = new port gain value; where the adjustment coefficient here is set according to the network state. The adjustment coefficient can be used to amplify or reduce the port gain value.
[0072] Specifically, when the current network state meets the set requirements, the adjustment coefficient is used to amplify the port gain value, and the value of the adjustment coefficient can be set to be greater than 1; or, when the current network state does not meet the set requirements, the adjustment coefficient is used to reduce the port gain value. For example, the value of the adjustment coefficient can be set to be greater than 0 and less than 1. The current network state here is determined according to the distribution of the port numbers used by the terminal device. The server can analyze the distribution of the ports used when the terminal device communicates with the server. If the distribution of the ports used by the terminal device multiple times is concentrated, it is considered that the current network state meets the set requirements, that is, the current network state is good. At this time, the adjustment coefficient is used to further amplify the port gain value of the port, so as to expand the port range of the target device, which is beneficial to further improving the probability of successful connection to the target device and the success rate of private network penetration; if the distribution of the ports used by the terminal device multiple times is discrete, it is considered that the current network state does not meet the set requirements, that is, the current network state is poor. At this time, the adjustment coefficient is used to reduce the port gain value, so as to reduce the port range of the target device, which is beneficial to saving the bandwidth resources between the terminal device and the target device.
[0073] The concentrated port distribution and the discrete port distribution are relative concepts and can be set according to different situations. For example, the concentrated distribution of ports used multiple times means using the same port number multiple times or the port numbers used multiple times are consecutive; the discrete distribution of port numbers used multiple times means that the port numbers used multiple times are different and non-consecutive. Another example is that the concentrated distribution of ports used multiple times means that the difference between every two closest port numbers among the port numbers used multiple times is less than the threshold; the discrete distribution of port numbers used multiple times means that the difference between two closest port numbers among the port numbers used multiple times is greater than the threshold, where the threshold value is, for example, 1 or 2.
[0074] Optionally, the server can further determine the new port range of the target device according to the new port gain value of the target device and the port number used by the target device for the last time, and then directly carry the new port range in the reconnection response and send it to the terminal device. The specific method for determining the new port range can refer to the relevant content of the above embodiments and will not be elaborated here. Optionally, the more the number of retries, the larger the new port range.
[0075] It should be noted that after receiving the reconnection request from the terminal device, the server can use different implementation methods to obtain the port gain value of the target device determined last time, specifically including but not limited to the following methods: The server finds the port gain value of the target device sent to the terminal device last time locally according to the device identifier of the terminal device. Or, the reconnection request carries the port gain value of the target device sent by the server last time. At this time, the server can obtain the port gain value by parsing the reconnection request.
[0076] It should also be noted that the server can also obtain the retry times of the terminal device and the target device in different implementation manners, specifically including but not limited to the following manners: The retry times can also be carried in the reconnection request, and the server obtains the retry times by parsing the reconnection request; or, the retry times are carried in the heartbeat packet information reported by the terminal device to the server, and the server determines the retry times through the heartbeat packet information of the terminal device; or, the server obtains the retry times sent by the terminal device in a certain manner.
[0077] Step S340: The server generates a reconnection response according to the new port information and sends the reconnection response to the terminal device.
[0078] Among them, the above reconnection response can carry a new port gain value and the first port number currently used by the target device, or the reconnection response carries a new port range.
[0079] It should be noted that the first port number and the second port number of the target device sent by the server to the terminal device twice can be the same port number or different port numbers.
[0080] Step S350: The terminal device receives the reconnection response sent by the server.
[0081] Step S360: The terminal device performs private network penetration on the target device again according to the new port information.
[0082] An embodiment of the present application provides a communication system, including: a server, a target device, and a terminal device.
[0083] The server communicates with the target device and the terminal device respectively.
[0084] The server includes a first processor and a first memory. The first memory stores machine-readable instructions executable by the first processor. When the machine-readable instructions executable by the first processor are executed by the first processor, the methods described in steps S110 to S130, steps S220 to S230, and steps S330 to S340 above are executed.
[0085] The terminal device includes a second processor and a second memory. The second memory stores machine-readable instructions executable by the second processor. When the machine-readable instructions executable by the second processor are executed by the second processor, the methods described in steps S210, S240, steps S310 to S320, and steps S350 to S360 above are executed.
[0086] It should be understood that the device corresponds to the terminal device in the above-mentioned embodiment and can execute each step performed by the terminal device involved in the above-mentioned method embodiment. The specific functions of the device can be found in the description above. To avoid repetition, a detailed description is omitted here. The device includes at least one software function module that can be stored in a memory in the form of software or firmware or fixed in the operating system (OS) of the device.
[0087] The embodiment of the present application also provides a computer program product, including computer program instructions, which, when read and executed by a processor, execute the method described above. It should be understood that the program product corresponds to the above-mentioned communication method embodiment and can execute the various steps performed by the server or terminal device involved in the above-mentioned method embodiment. The specific functions of the program product can be found in the description above. To avoid repetition, detailed description is appropriately omitted here. The program product includes at least one software function module that can be stored in a memory in the form of software or firmware or solidified in the operating system (OS) of the program product.
[0088] An electronic device provided in an embodiment of the present application includes: a processor and a memory, the memory storing machine-readable instructions executable by the processor, and the machine-readable instructions, when executed by the processor, performing the method executed by the server or terminal device as described above.
[0089] The embodiment of the present application also provides a computer-readable storage medium having a computer program stored thereon, and the computer program is executed by a processor to execute the method executed by the server or terminal device as described above. The computer-readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0090] In several embodiments provided by the embodiments of the present application, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely illustrative. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architectures, functions, and operations of devices, methods, and computer program products according to multiple embodiments of the present application. In this regard, each block in the flowchart or block diagram may represent a module, a program segment, or a part of code, and a module, a program segment, or a part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may also occur in a different order from that marked in the accompanying drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in the reverse order, mainly depending on the functions involved.
[0091] In addition, the various functional modules in the embodiments of the present application may be integrated together to form an independent part, or each module may exist alone, or two or more modules may be integrated to form an independent part.
[0092] In this document, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations.
[0093] The above description is only an alternative implementation manner of the embodiments of the present application, but the protection scope of the embodiments of the present application is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the embodiments of the present application, and all of them should be covered within the protection scope of the embodiments of the present application.
Claims
1. A communication method, characterized in that, Including: Receiving a first connection request sent by a terminal device, where the first connection request is used to connect to a target device located in another local area network; Obtaining the number of devices in the local area network where the target device is located, and determining the port information of the target device according to the number of devices, where the port information includes a port gain value or a port range, and the port information is used to determine the port range of the target device; Sending a first connection response to the terminal device, where the first connection response carries the port information of the target device; Wherein, the communication method further includes: receiving a reconnection request sent by the terminal device for the target device; adjusting the port information of the target device to obtain new port information, where the new port information includes a new port gain value; sending a reconnection response to the terminal device, where the reconnection response carries the new port gain value and the first port number that the target device is currently using; Adjusting the port information to obtain new port information includes: obtaining the number of retry attempts for the connection between the terminal device and the target device, and using the product of the port gain value and the number of retry attempts as the new port gain value; or, obtaining the number of retry attempts for the connection between the terminal device and the target device and an adjustment coefficient corresponding to the current network state, and using the product of the port gain value, the number of retry attempts, and the adjustment coefficient as the new port gain value.
2. The method according to claim 1, wherein The first connection request carries a unique identification number of the target device; obtaining the number of devices in the local area network where the target device is located includes: Determining the public network IP of the local area network where the target device is located according to the unique identification number of the target device; Obtaining the number of devices corresponding to the public network IP.
3. The method according to claim 2, wherein The method further includes: According to the public network IP and the unique identification number of the device carried in the received heartbeat packet, counting the number of unique identification numbers corresponding to each public network IP, and determining the number of devices corresponding to each public network IP as the number of devices corresponding to each public network IP.
4. The method according to claim 1, characterized in that, The new port gain value is determined according to the number of retry attempts for the connection of the target device; where the more the number of retry attempts, the larger the new port gain value.
5. The method according to claim 1, wherein The values of the adjustment coefficients corresponding to different network states are different; When the current network state meets the set requirements, the adjustment coefficient is used to amplify the port gain value; or, when the current network state does not meet the set requirements, the adjustment coefficient is used to reduce the port gain value; Where the current network state is determined according to the distribution of the port numbers used by the terminal device.
6. A communication method, characterized in that, Applied to a terminal device, including: Sending a first connection request to a server, where the first connection request is used to connect to a target device located in another local area network; Receiving a first connection response sent by the server, where the first connection response carries the port information of the target device, and the port information includes a port gain value or a port range; Attempting to connect to the target device according to the port information; Wherein, the communication method further includes: if a second connection response returned by the target device is not received within a predetermined duration, sending a reconnection request for the target device to the server, where the reconnection request is used to adjust the port information to obtain new port information; receiving a reconnection response sent by the server, and attempting to reconnect with the target device according to the reconnection response; the reconnection response carries a new port gain value of the target device and a first port number that the target device has most recently used; The adjusting the port information to obtain new port information includes: obtaining the number of retry attempts for the connection between the terminal device and the target device, and using the product of the port gain value and the number of retry attempts as the new port gain value; or, obtaining the number of retry attempts for the connection between the terminal device and the target device and an adjustment coefficient corresponding to the current network state, and using the product of the port gain value, the number of retry attempts, and the adjustment coefficient as the new port gain value.
7. The method according to claim 6, wherein The port information includes the port gain value, and the first connection response further carries a second port number that the target device has most recently used; the attempting to connect with the target device according to the port information includes: Determining an upper limit value of the port range of the target device as the sum of the second port number of the target device and the port gain value, and determining a lower limit value of the port range of the target device as the difference between the second port number of the target device and the port gain value; Determining the port range of the target device according to the upper limit value and the lower limit value of the port range; Attempting to connect with the target device according to the port range of the target device.
8. The method according to claim 7, wherein The attempting to connect with the target device according to the port range includes: Generating a plurality of second connection requests according to the public IP of the target device and a plurality of port numbers in the port range; Sending the plurality of second connection requests to the target device.
9. A communication system, characterized in that, Comprising: A server, a target device, and a terminal device; the server communicates with the target device and the terminal device respectively; The server includes a first processor and a first memory, the first memory stores machine-readable instructions executable by the first processor, and when the machine-readable instructions executable by the first processor are executed by the first processor, the method according to any one of claims 1 to 5 is executed; The terminal device includes a second processor and a second memory, the second memory stores machine-readable instructions executable by the second processor, and when the machine-readable instructions executable by the second processor are executed by the second processor, the method according to any one of claims 6 to 8 is executed.
10. A computer program product, characterized in that, Comprising computer program instructions, when the computer program instructions are read and run by a processor, the method according to any one of claims 1 to 5 is executed, or the method according to any one of claims 6 to 8 is executed.
11. An electronic device, characterized in that, Comprising: A processor and a memory, the memory storing machine-readable instructions executable by the processor, and when the machine-readable instructions are executed by the processor, the method according to any one of claims 1 to 5 is performed; or, when the machine-readable instructions are executed by the processor, the method according to any one of claims 6 to 8 is performed.
12. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, and when the computer program is run by a processor, the method according to any one of claims 1 to 5 is performed; or, when the computer program is run by the processor, the method according to any one of claims 6 to 8 is performed.
Citation Information
Patent Citations
P2P penetration method based on port prediction, electronic equipment and medium
CN111405052A