Port allocation method, apparatus, and electronic device

By employing a combined horizontal and vertical port allocation method in the NAT gateway, the problem of port resource exhaustion was solved, port allocation efficiency and user experience were improved, and stable connections were ensured in high-concurrency environments.

CN116846872BActive Publication Date: 2026-07-21BEIJING KINGSOFT CLOUD NETWORK TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIJING KINGSOFT CLOUD NETWORK TECH CO LTD
Filing Date
2022-03-23
Publication Date
2026-07-21

AI Technical Summary

Technical Problem

In existing technologies, NAT gateways are prone to port blocking when allocating IP addresses and ports, leading to exhaustion of port resources, low allocation efficiency, and poor user experience, especially when there are many concurrent requests and the requests are long TCP connections.

Method used

A port allocation method is adopted, which searches for available ports by polling among multiple IPs. First, it jumps horizontally among the IPs to find available ports. When a blockage is encountered, it jumps vertically to search for ports within the same IP. This avoids the vertical exhaustion of port resources and improves port allocation efficiency.

Benefits of technology

It effectively improves port allocation efficiency, reduces port conflict rate and request forwarding failure rate, enhances user experience, and ensures stable connection quality under high concurrency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116846872B_ABST
    Figure CN116846872B_ABST
Patent Text Reader

Abstract

The application provides a port allocation method, device and electronic equipment, receives a first access request, and detects whether a first port of a current IP is occupied; if the first port is not occupied, allocates the first access request to the first port; when a second access request is received, detects whether a second port of a next IP of the current IP is occupied to allocate the second access request; if the first port is occupied, detects whether a next port of the first port of the current IP is occupied; if the next port of the first port is not occupied, allocates the first access request to the next port of the first port. In the port allocation mode, the process of searching for available ports is changed, that is, available ports are searched among the IPs in turn, and when a port is occupied, available ports are searched from the ports of the IP corresponding to the occupied port. The mode effectively improves the port allocation efficiency, reduces the port conflict rate, and thus improves the user experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data processing technology, and in particular to a port allocation method, apparatus, and electronic device. Background Technology

[0002] The allocation of IP (Internet Protocol Address) and PORT (port) addresses by a NAT (Network Address Translation) gateway is its core and most fundamental function. Ensuring the most efficient allocation of IP and PORT combinations is crucial to the performance and user experience of a NAT gateway. The mainstream allocation scheme in public clouds typically involves customers purchasing a certain number of public IP addresses, each corresponding to multiple PORTs. The NAT gateway then allocates different IP + PORT combinations based on the different services accessed from within the internal network.

[0003] In related technologies, when allocating IP addresses and ports, requests are allocated sequentially, starting with the first port of the first IP address. Once all ports of the first IP address are allocated, allocation begins again with the first port of the second IP address, and so on, until the last port of the last IP address is allocated. Then, allocation returns to the first port of the first IP address. When there are many concurrent requests to the same service, and many of these requests are long-lived TCP (Transmission Control Protocol) connections, port resources are quickly exhausted, leading to port congestion during allocation. When port congestion occurs, the allocation process proceeds down four port positions. If no free port is found after four deferred attempts, the request forwarding fails. Even if a subsequent port becomes available after four deferred attempts, it will not be allocated, resulting in low allocation efficiency and a poor user experience. Summary of the Invention

[0004] The purpose of this invention is to provide a port allocation method, apparatus, and electronic device to improve port allocation efficiency and enhance user experience.

[0005] In a first aspect, the present invention provides a port allocation method, which is applied to a designated gateway, the designated gateway containing multiple IPs, each IP corresponding to multiple ports; the method includes: receiving a first access request, detecting whether a first port of the current IP is occupied; wherein, the first port includes: the next port of the port previously allocated to the current IP; if the first port is not occupied, allocating the first access request to the first port; when a second access request is received, detecting whether a second port of the next IP of the current IP is occupied, and allocating the second access request; if the first port is occupied, detecting whether the next port of the first port corresponding to the current IP is occupied; if the next port of the first port is not occupied, allocating the first access request to the next port of the first port.

[0006] In an optional implementation, the method further includes: if the next port of the first port is occupied, sequentially checking whether a specified number of ports after the next port of the first port are occupied; if there are unoccupied ports, allocating the first access request to the unoccupied ports; if all specified number of ports are occupied, determining that the forwarding of the first access request has failed.

[0007] In an optional implementation, the method further includes: detecting whether the next port of the next port of the first port is occupied; if the next port of the next port of the first port is not occupied, allocating the first access request to the next port of the next port of the first port; if the next port of the next port of the first port is occupied, determining whether the number of detections has reached a preset threshold; if it has, determining that the first access request forwarding has failed; if it has not, determining the next port of the next port of the next port of the first port as the new next port of the next port of the first port, and continuing to execute the step of detecting whether the next port of the next port of the first port is occupied.

[0008] In an optional implementation, the step of detecting whether the second port of the next IP of the current IP is occupied when a second access request is received, so as to allocate the second access request, includes: when a second access request is received, determining the second access request as a new first access request, determining the second port of the next IP of the current IP as a new first port of the current IP, and continuing to execute the steps of receiving the first access request and detecting whether the first port of the current IP is occupied, so as to allocate a port for the second access request; wherein, the second port includes: the port following the port that was previously allocated corresponding to the next IP of the current IP.

[0009] In an optional implementation, after the step of detecting whether the second port of the next IP of the current IP is occupied when a second access request is received, so as to allocate the second access request, the method includes: if a third access request is received, detecting whether the third port of the next IP of the next IP of the current IP is occupied, so as to allocate the third access request; wherein, among the multiple IPs included in the specified gateway, the first IP is regarded as the next IP of the last IP; the third port includes: the next port of the port that was previously allocated corresponding to the next IP of the next IP of the next IP of the current IP.

[0010] In an optional implementation, the specified gateway mentioned above includes a NAT gateway.

[0011] In an optional implementation, after the step of detecting whether the second port of the next IP of the current IP is occupied when a second access request is received, so as to allocate the second access request, the method further includes: if the second access request is forwarded, releasing the port occupied by the second access request.

[0012] Secondly, the present invention provides a port allocation device, which is installed at a designated gateway, the designated gateway containing multiple IPs, each IP corresponding to multiple ports; the device includes: a request receiving module, configured to receive a first access request and detect whether a first port of the current IP is occupied; wherein, the first port includes: the next port of the port previously allocated to the current IP; a request allocation module, configured to allocate the first access request to the first port if the first port is not occupied; when a second access request is received, to detect whether a second port of the next IP of the current IP is occupied, so as to allocate the second access request; and a port detection module, configured to detect whether the next port of the first port corresponding to the current IP is occupied if the first port is occupied; and to allocate the first access request to the next port of the first port if the next port of the first port is not occupied.

[0013] Thirdly, the present invention provides an electronic device including a processor and a memory, the memory storing machine-executable instructions that can be executed by the processor, the processor executing the machine-executable instructions to implement the above-described port allocation method.

[0014] Fourthly, the present invention provides a machine-readable storage medium storing machine-executable instructions, which, when invoked and executed by a processor, cause the processor to implement the aforementioned port allocation method.

[0015] The embodiments of the present invention bring the following beneficial effects:

[0016] This invention provides a port allocation method, apparatus, and electronic device. The method is applied to a designated gateway, which includes multiple IPs, each IP corresponding to multiple ports. First, a first access request is received, and it is checked whether the first port of the current IP is occupied. If the first port is not occupied, the first access request is allocated to the first port. When a second access request is received, it is checked whether the second port of the next IP of the current IP is occupied, and the second access request is allocated accordingly. If the first port is occupied, it is checked whether the next port corresponding to the first port of the current IP is occupied. If the next port is not occupied, the first access request is allocated to the next port of the first port. In this method, the process of finding available ports during port allocation is variable. First, available ports are searched sequentially among the IPs. When a port is occupied, an available port is searched from the ports under the IP corresponding to the occupied port. This method effectively improves port allocation efficiency, reduces port conflict rate, and reduces request forwarding failure rate, thereby improving user experience.

[0017] Other features and advantages of the invention will be set forth in the following description, or some features and advantages may be inferred from the description or determined without doubt, or may be learned by practicing the techniques described above.

[0018] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description

[0019] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0020] Figure 1 A schematic diagram illustrating the allocation of an IP and PORT combination provided in related technologies;

[0021] Figure 2 A schematic diagram illustrating the allocation of IP and port combinations provided in related technologies;

[0022] Figure 3 A flowchart of a port allocation method provided in an embodiment of the present invention;

[0023] Figure 4 A flowchart of another port allocation method provided in an embodiment of the present invention;

[0024] Figure 5A port allocation diagram provided for an embodiment of the present invention;

[0025] Figure 6 This is a schematic diagram of the structure of a port allocation device provided in an embodiment of the present invention;

[0026] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention. Detailed Implementation

[0027] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.

[0028] Therefore, the following detailed description of the embodiments of the invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to illustrate selected embodiments of the invention. All other embodiments obtained by those skilled in the art based on the embodiments of the invention without inventive effort are within the scope of protection of the invention.

[0029] IP and PORT allocation is the core and most basic function of a NAT gateway. Ensuring the most efficient allocation of IP and PORT combinations is crucial for NAT gateway performance and user experience. The mainstream allocation solution in public clouds typically involves customers purchasing a certain number of public IPs, each corresponding to multiple PORTs. The NAT gateway can allocate different IP + PORT combinations (also known as NATIP + NATPort combinations) based on different services accessed from within the internal network. The main processing flow is as follows:

[0030] 1. Establish a dynamic session for each new connection and save the five-tuple information before and after NAT translation. The five-tuple information here includes the source IP address, source port, destination IP address (dip), destination port (dprot), and transport protocol.

[0031] 2. Ensure that SNAT (which can be called source address) translation achieves a 1:1 mapping, that is, the five-tuple information after NAT translation of different connections cannot be the same. Otherwise, when the public network response message returns to the NAT, the session lookup will result in 1:N mapping combinations.

[0032] Assume the NAT gateway has three IPs, and internal network clients (referred to as clients) are accessing the same web server concurrently (equivalent to...). Figure 1If the web server in the network receives many requests to access the same destination IP and port, and the internal QPS (Query Per Second) is high, and most of these requests are for the same service (dip + dport are fixed), to ensure a 1:1 consistency before and after NAT translation, it is necessary to ensure that the natip + natport combination is unique for each query (equivalent to an access request). Figure 1 The diagram shown illustrates the allocation of an IP and PORT combination provided in related technologies.

[0033] Figure 1 The internal network query contains four requests, with the source IP addresses and ports assigned as follows: IP address 1.1.1.1 and port 1024, IP address 1.1.1.1 and port 1025, IP address 1.1.1.1 and port 1026, and IP address 1.1.1.2 and port 1024. The NAT gateway has three corresponding IP addresses: 10.1.2.1, 10.1.2.2, and 10.1.2.3. The IP and PROT combinations in the NAT gateway for each request are as follows: IP address 10.1.2.1 and port 2048, IP address 10.1.2.2 and port 2048, IP address 10.1.2.3 and port 2048, and IP address 10.1.2.1 and port 2049.

[0034] In related technologies, assuming the NAT gateway corresponds to three IPs: IP1 (10.1.2.1), IP2 (10.1.2.2), and IP3 (10.1.2.3), each IP has multiple ports; when allocating IPs and ports, the first request allocates the first port (1024) of IP1, the second request allocates the second port (1025) of IP1, and so on. Once the last available port (65535) of IP1 is allocated, the process jumps to IP2 for allocation, and so on, until the last port (65535) of IP3 is allocated, returning to the first port of IP1, and so on. Figure 2 The diagram showing the allocation of IP and port combinations will sequentially extract a pair of available NATIP+port combinations in a vertical column pattern and perform SNAT translation.

[0035] In related technologies, when there are many concurrent requests to the same service, and most of these requests are long-lived TCP connections (which occupy ports for a long time), the combined resources of NATIP and NATPort are quickly exhausted, causing a "port rollback" phenomenon, also known as port conflict. However, if the release rate of old connections is greater than the request rate of new connections (the number of short connections is greater than the number of long connections, and each time a short connection is released, NAT releases a pair of NATIP+Port resources), then theoretically, NAT port resources will act like a reservoir, with the rate of filling exceeding the rate of draining, thus achieving a dynamic balance and a surplus, and there will be no port conflict problem. However, in actual allocation, if some consecutively allocated long connections are never released, and the NATIP+NATPort selection algorithm returns to this position after going around once, it will cause port blocking. At this point, if the allocation method provided in the relevant technologies is still followed, it will result in continuously finding NAT+port combinations that are still in use. However, the program cannot continue to try and fail indefinitely. Currently, the default NAT setting is to search three times consecutively; if a valid NAT+port combination is not found, NAT forwarding will fail. But in this scenario, if... Figure 2 As shown, as long as you skip the ports in the gray area, you can still be assigned a usable NATIP+port combination.

[0036] Increasing the number of NATIPs in related technologies has not alleviated this problem because each NATIP allocates ports vertically, and the process switches to the next NATIP only after one is used up. If a port of a certain NATIP is blocked, then ports of subsequent NATIPs cannot be allocated, resulting in a very poor user experience.

[0037] To address the aforementioned problems, embodiments of the present invention provide a port allocation method, apparatus, and electronic device. This technology can be applied to request forwarding scenarios in NAT gateways, particularly to IP and port allocation scenarios in NAT gateways. To facilitate understanding of this embodiment, a port allocation method disclosed in this invention will first be described in detail. This method is applied to a designated gateway, which contains multiple IPs (also referred to as NATIPs), and each IP corresponds to multiple ports; for example... Figure 3 As shown, the method includes the following specific steps:

[0038] Step S302: Receive the first access request and check whether the first port of the current IP is occupied; if the first port is occupied, proceed to step S304; if the first port is not occupied, proceed to step S306.

[0039] In practical implementation, the specified gateway can be a NAT gateway or other gateway capable of forwarding requests. This specified gateway contains multiple IPs, the number of which can be determined according to user needs. Each IP corresponds to multiple ports; for example, the port numbers corresponding to an IP can range from 1024 to 65535. The first access request can be any access request sent by a client, which can be a mobile terminal (phone, tablet, etc.) or a computer. The current IP is the next IP after the IP assigned in the previous request. The first port of the current IP can be the next port after the port previously assigned to the current IP. For example, if the previously assigned port for the current IP was 1024, then the first port for the current IP would be 1025.

[0040] In practical applications, the second IP address among multiple IP addresses can be used as the next IP address after the first IP address, the third IP address as the next IP address after the second IP address, and so on, until the first IP address is used as the next IP address after the last IP address.

[0041] Step S304: Check if the next port of the first port corresponding to the current IP is occupied; if the next port of the first port is not occupied, assign the first access request to the next port of the first port.

[0042] When the first port corresponding to the current IP is occupied, it is necessary to check whether the next port under the first port of the current IP is occupied. If the next port under the first port of the current IP is not occupied, the first access request is assigned to the next port of the first port. If the next port under the first port of the current IP is occupied, it is necessary to continue to check whether the next port of the next port of the next port of the current IP is occupied. If an occupied port is found within the specified number of checks, the first access request is assigned to this unoccupied interface. If no unoccupied port is found after the specified number of checks, a message indicating that the request forwarding failed will be returned.

[0043] In practice, if the first port corresponding to the current IP is detected to be occupied, the system will continue to check whether the ports after the first port under the current IP are occupied, until an unoccupied port is found under the current IP or the specified number of checks is reached, and then return a forwarding failure message.

[0044] Step S306: Assign the first access request to the first port; proceed to step S308.

[0045] Step S308: When a second access request is received, check whether the second port of the next IP of the current IP is occupied, so as to allocate the second access request.

[0046] When the first port of the current IP is idle, the first access request is assigned to the first port, and the system continues to listen for new access requests sent to the designated gateway. If a new request is received, it is identified as the second access request, and a port is assigned to it. During port allocation, it is necessary to check whether the second port of the next IP of the current IP is occupied. If it is not occupied, the second access request is assigned to the second port; if it is occupied, it is checked whether the port following the second port of the next IP of the current IP is occupied, in order to find an idle port to allocate the second access request. Here, the second port is the port following the last allocated port of the next IP of the current IP.

[0047] In a specific implementation, the second access request can be the access request received after the first access request has been allocated. When allocating a port for the second access request, the first access request in step S302 can be regarded as the second access request. The first port of the current IP is replaced with the second port of the next IP of the current IP, and steps S302-S306 are continued to allocate a port for the second access request.

[0048] After the second access request is assigned, the new access request received by NAT can also be used as a new second access request. However, when the new second access request is assigned a port, the previous second access request can be regarded as the first access request, so that the port can be assigned to the new second access request through step S308.

[0049] In practical applications, after an access request is assigned to a port, upon receiving another access request, the port will be assigned starting from the IP address following the IP address of the previous access request. If a port is occupied during the port allocation process, the system will check if the port following the occupied port of the current IP address is free. In other words, when a port is occupied, the system will search for a free port among the ports under the current IP address to complete the port allocation.

[0050] This method, when allocating ports for multiple consecutive long-lived connections, can horizontally span multiple IPs to allocate ports corresponding to multiple IPs. If a long-lived connection occupies a port for an extended period, it uses a horizontally allocated port. When allocating a port for a new request and encountering a occupied port, it can switch to vertical hopping (i.e., sequentially searching for available ports under the IPs corresponding to the occupied ports), thus quickly bypassing the congestion zone. However, related technologies have consistently used vertical port allocation. Skipping congestion zones requires traversing a large number of ports, making it difficult to jump and prone to port allocation failures.

[0051] This invention provides a port allocation method that first receives a first access request and checks whether the first port of the current IP is occupied. If the first port is not occupied, the first access request is allocated to the first port. When a second access request is received, it checks whether the second port of the next IP of the current IP is occupied, and allocates the second access request accordingly. If the first port is occupied, it checks whether the next port of the first port corresponding to the current IP is occupied. If the next port of the first port is not occupied, the first access request is allocated to the next port of the first port. In this method, the process of finding available ports changes during port allocation. First, available ports are searched sequentially among all IPs. When a port is occupied, an available port is searched from the ports under the IP corresponding to the occupied port. This method effectively improves port allocation efficiency, reduces port conflict rate, and reduces request forwarding failure rate, thereby improving user experience.

[0052] Corresponding to the above method embodiments, this invention also provides another port allocation method. This method is implemented based on the above method embodiments. The method focuses on describing the specific process of detecting whether the second port of the next IP address of the current IP is occupied when a second access request is received, in order to allocate the second access request (implemented through steps S414-S416 below); for example... Figure 4 As shown, the method includes the following specific steps:

[0053] Step S402: Receive the first access request and check whether the first port of the current IP is occupied; if the first port is not occupied, proceed to step S404; if the first port is occupied, proceed to step S406.

[0054] Step S404: Assign the first access request to the first port; proceed to step S412.

[0055] Step S406: Check if the next port of the first port corresponding to the current IP is occupied; if it is not occupied, proceed to step S408; otherwise, proceed to step S410.

[0056] Step S408: Assign the first access request to the next port after the first port, and proceed to step S412.

[0057] Step S410: Check sequentially whether a specified number of ports following the first port are occupied; if there are unoccupied ports, assign the first access request to an unoccupied port; if all specified number of ports are occupied, determine that the first access request forwarding has failed.

[0058] The specified number can be set according to R&D needs. For example, it can be set to two or three. Setting a specified number of ports to be detected can effectively avoid the waste of resources from unlimited searches. It can also be understood as preventing the situation where there are no available ports in the gateway, resulting in ports being occupied indefinitely. In the specific implementation, the process involves sequentially checking whether a specified number of ports after the first port are occupied. When an unoccupied port is detected, the first access request is assigned to that port.

[0059] In practical applications, step S410 above can be achieved through the following steps 10-13:

[0060] Step 10: Check if the port after the port after the first port is occupied; if the port after the port after the first port is not occupied, proceed to step 11; otherwise, proceed to step 12.

[0061] Step 11: Assign the first access request to the port after the port after the port after the first port.

[0062] Step 12: Determine whether the number of detections has reached the preset threshold. If it has, proceed to step 13; otherwise, proceed to step 14.

[0063] The aforementioned preset threshold can be determined based on the specified quantity, which is equivalent to determining whether the detected vegetables have reached the preset threshold, or determining whether the number of detected ports has reached the specified quantity.

[0064] Step 13: Determine that the first access request forwarding failed.

[0065] Step 14: Determine the next port of the next port of the first port as the new next port of the next port of the first port, and continue to execute the step of checking whether the next port of the next port of the first port is occupied.

[0066] Step S412: Listen for new access requests. If a new access request is received, execute step S414. Otherwise, continue listening (or continue executing step S412).

[0067] Step S414: The received new request is identified as the second access request.

[0068] Step S416: Determine the second access request as the new first access request, determine the second port of the next IP of the current IP as the new first port of the current IP, and execute step S402.

[0069] The aforementioned second port includes the port following the last port assigned to the next IP address of the current IP. In practical implementation, once a port is assigned to the second access request, the port occupied by the second access request will be released if the second access request is successfully forwarded. This second access request can be understood as any request received.

[0070] In practical applications, after a port is allocated for the second access request, if a third access request is received, it checks whether the third port of the next IP address of the current IP is occupied, and then allocates the third access request accordingly. Among the multiple IPs included in the specified gateway, the first IP is designated as the next IP address of the last IP address. This third port includes the port following the previously allocated port of the next IP address of the next IP address of the current IP. In other words, after each new access request, an available port is searched sequentially among the various IP addresses.

[0071] To facilitate understanding of the embodiments of the present invention, as follows: Figure 5 The diagram shown illustrates a port allocation method. Figure 5 The NAT gateway in the system contains four IPs, designated as IP1 (10.1.2.1), IP2 (10.1.2.2), IP3 (10.1.2.3), and IP4 (10.1.2.4). Each IP has ports numbered from 1024 to 65535. Upon receiving a new connection (equivalent to a new access request), the gateway polls each IP in turn to find an available port; this process is called "lateral hop." For example, after receiving the first access request, the NAT gateway assigns it to the combination of IP1 (10.1.2.1) + PORT (1024). After receiving the second access request, it assigns it to the combination of IP2 (10.1.2.2) + PORT (1024), and so on, until it assigns port 65535 to IP4. Then, upon receiving a new access request, it assigns the new request to port 1024 of IP1.

[0072] During the dynamic connection and release process of services, the available free ports under each IP may be different, such as Figure 5 As shown, at a certain moment, the in-use ports (equivalent to occupied ports) of IP1 (10.1.2.1) are 1026 and 1027 (free ports are the ports after removing the in-use ports), while the in-use ports of IP2 (10,1.2.2) are 1025 and 1026.

[0073] If some consecutive hot connections are not released for a long time ( Figure 5The gray area (corresponding to the port in the diagram) will block the "lateral jump" method of finding available ports. Instead, it will switch to "vertical jump" (equivalent to searching vertically along the port sequence only within a specific IP address) to find available ports. This efficiently avoids hotspots left over from the lateral jump allocation method between IPs, similar to seeing a congested red road in navigation and taking a detour to another clear green road. This method effectively maximizes the utilization of each available port. While hotspots may change dynamically over time, the time complexity of finding an available IP + PROT combination remains constant, thus improving port allocation efficiency.

[0074] The aforementioned port allocation method, when horizontal lookup is blocked, continues vertical lookup. This interleaved filtering of IP and PORT combinations effectively improves port lookup efficiency and port utilization, significantly reduces port conflict rates, minimizes gateway conversion failures, and lowers packet loss rates. Consequently, services passing through the designated gateway experience better performance and smoother connection quality. Furthermore, this method also addresses issues such as severe port conflicts, high request conversion failure rates, repeated packet retransmissions, and slow download speeds during peak traffic periods at the designated gateway.

[0075] Regarding embodiments of the above-described port allocation method, this embodiment of the invention provides a port allocation device. This device is installed at a designated gateway, which contains multiple IP addresses, each IP address corresponding to multiple ports; for example... Figure 6 As shown, the device includes:

[0076] The request receiving module 70 is used to receive the first access request and detect whether the first port of the current IP is occupied; wherein, the first port includes: the port following the port that was last allocated to the current IP;

[0077] The request allocation module 71 is used to allocate the first access request to the first port if the first port is not occupied; when a second access request is received, it checks whether the second port of the next IP of the current IP is occupied in order to allocate the second access request.

[0078] The port detection module 72 is used to detect whether the next port corresponding to the first port of the current IP is occupied if the first port is occupied; if the next port of the first port is not occupied, the first access request is assigned to the next port of the first port.

[0079] The aforementioned port allocation device first receives a first access request and checks whether the first port of the current IP is occupied. If the first port is not occupied, the first access request is allocated to the first port. When a second access request is received, it checks whether the second port of the next IP of the current IP is occupied, and allocates the second access request accordingly. If the first port is occupied, it checks whether the next port of the first port corresponding to the current IP is occupied. If the next port of the first port is not occupied, the first access request is allocated to the next port of the first port. In this method, the process of finding available ports varies. It first polls each IP sequentially to find available ports. When a port is occupied, it searches for available ports from the ports under the IP corresponding to the occupied port. This method effectively improves port allocation efficiency, reduces port conflict rate, and reduces request forwarding failure rate, thereby improving user experience.

[0080] Furthermore, the aforementioned port detection module 72 is also configured to: if the next port of the first port is occupied, sequentially detect whether a specified number of ports after the next port of the first port are occupied; if there are unoccupied ports, assign the first access request to the unoccupied ports; if all specified number of ports are occupied, determine that the forwarding of the first access request has failed.

[0081] In a specific implementation, the port detection module 72 is further configured to: detect whether the port following the port of the first port is occupied; if the port following the port of the first port is not occupied, assign the first access request to the port following the port of the first port; if the port following the port of the first port is occupied, determine whether the number of detections has reached a preset threshold; if it has, determine that the first access request forwarding has failed; if it has not, determine the port following the port of the first port as the new port following the port of the first port, and continue to execute the step of detecting whether the port following the port of the first port is occupied.

[0082] Furthermore, the above-mentioned request allocation module 71 is also used to: when a second access request is received, determine the second access request as a new first access request, determine the second port of the next IP of the current IP as the new first port of the current IP, continue to execute the steps of receiving the first access request and detecting whether the first port of the current IP is occupied, so as to allocate a port for the second access request; wherein, the second port includes: the port following the port that was previously allocated to the next IP of the current IP.

[0083] Furthermore, the aforementioned request allocation module 71 is also configured to: upon receiving a second access request, detect whether the second port of the next IP of the current IP is occupied, and allocate the second access request; and if a third access request is received, detect whether the third port of the next IP of the next IP of the current IP is occupied, and allocate the third access request; wherein, among the multiple IPs included in the specified gateway, the first IP is designated as the next IP of the last IP; the third port includes: the port following the port previously allocated corresponding to the next IP of the next IP of the next IP of the current IP.

[0084] In specific implementations, the specified gateway mentioned above includes NAT gateways.

[0085] Furthermore, the above-mentioned device also includes a port release module, which is used to detect whether the second port of the next IP of the current IP is occupied when a second access request is received, so as to release the port occupied by the second access request after the second access request is allocated and if the second access request is forwarded.

[0086] The port allocation device provided in this embodiment of the invention has the same implementation principle and technical effect as the aforementioned method embodiment. For the sake of brevity, any parts not mentioned in the device embodiment can be referred to the corresponding content in the aforementioned method embodiment.

[0087] This invention also provides an electronic device, such as... Figure 7 As shown, the electronic device includes a processor 101 and a memory 100. The memory 100 stores machine-executable instructions that can be executed by the processor 101. The processor 101 executes the machine-executable instructions to implement the port allocation method described above.

[0088] Furthermore, Figure 7 The electronic device shown also includes a bus 102 and a communication interface 103, with the processor 101, the communication interface 103 and the memory 100 connected via the bus 102.

[0089] The memory 100 may include high-speed random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Communication between this system network element and at least one other network element is achieved through at least one communication interface 103 (which can be wired or wireless), such as the Internet, wide area network, local area network, or metropolitan area network. The bus 102 may be an ISA bus, PCI bus, or EISA bus, etc. The bus can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 7 The symbol is represented by a single double-headed arrow, but this does not mean that there is only one bus or one type of bus.

[0090] Processor 101 may be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method can be completed by the integrated logic circuitry in the hardware of processor 101 or by instructions in software form. Processor 101 can be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc.; it can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this invention. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this invention can be directly manifested as execution by a hardware decoding processor, or execution by a combination of hardware and software modules in the decoding processor. The software module can reside in a readily available storage medium in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, or registers. This storage medium is located in memory 100, and processor 101 reads information from memory 100 and, in conjunction with its hardware, completes the steps of the method described in the foregoing embodiments.

[0091] This invention also provides a machine-readable storage medium storing machine-executable instructions. When these machine-executable instructions are called and executed by a processor, they cause the processor to implement the aforementioned port allocation method. For specific implementation details, please refer to the method embodiments, which will not be repeated here.

[0092] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, essentially, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0093] In the description of this invention, it should be noted that the terms "center," "upper," "lower," "left," "right," "vertical," "horizontal," "inner," and "outer," etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are used only for the convenience of describing the invention and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on the invention. Furthermore, the terms "first," "second," and "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.

[0094] Finally, it should be noted that the above-described embodiments are merely specific implementations of the present invention, used to illustrate the technical solutions of the present invention, and not to limit it. The scope of protection of the present invention is not limited thereto. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments within the technical scope disclosed in the present invention, or make equivalent substitutions for some of the technical features; and these modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be covered within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.

Claims

1. A port allocation method, characterized in that, The method is applied to a specified gateway, which contains multiple IPs, each IP corresponding to multiple ports; the method includes: Upon receiving a first access request, the system checks whether the first port of the current IP is occupied; wherein, the first port includes: the port following the port previously allocated to the current IP; wherein, the current IP is the next IP corresponding to the IP after the previous request was allocated; If the first port is not occupied, the first access request is assigned to the first port; when a second access request is received, it is checked whether the second port of the next IP of the current IP is occupied, so as to assign the second access request; the second port is the port after the port that was last assigned to the next IP of the current IP. If the first port is occupied, check whether the next port corresponding to the first port of the current IP is occupied; if the next port of the first port is not occupied, assign the first access request to the next port of the first port.

2. The method according to claim 1, characterized in that, The method further includes: If the next port after the first port is occupied, check in turn whether a specified number of ports after the next port of the first port are occupied; if there are unoccupied ports, assign the first access request to the unoccupied ports; if all of the specified number of ports are occupied, determine that the first access request forwarding has failed.

3. The method according to claim 2, characterized in that, The method further includes: Detect whether the port following the port of the first port is occupied; if the port following the port of the first port is not occupied, assign the first access request to the port following the port of the first port. If the port after the port of the first port is occupied, determine whether the number of detections has reached a preset threshold. If it has, determine that the first access request forwarding has failed. If not, the next port of the next port of the first port is determined as the new next port of the next port of the first port, and the step of detecting whether the next port of the next port of the first port is occupied continues.

4. The method according to claim 1, characterized in that, The step of detecting whether the second port of the next IP of the current IP is occupied when a second access request is received, so as to allocate the second access request, includes: When the second access request is received, the second access request is determined as a new first access request, the second port of the next IP of the current IP is determined as the new first port of the current IP, and the steps of receiving the first access request and detecting whether the first port of the current IP is occupied are continued, so as to allocate a port for the second access request. The second port includes the port following the previously assigned port corresponding to the next IP of the current IP.

5. The method according to claim 1 or 4, characterized in that, After the step of detecting whether the second port of the next IP of the current IP is occupied when a second access request is received, so as to allocate the second access request, the method includes: If a third access request is received, check whether the third port of the next IP of the next IP of the current IP is occupied, and allocate the third access request accordingly; Among the multiple IPs included in the designated gateway, the first IP is the next IP after the last IP; the third port includes the port after the last port corresponding to the next IP of the next IP of the current IP.

6. The method according to claim 1, characterized in that, The designated gateway includes a NAT gateway.

7. The method according to claim 1, characterized in that, After the step of detecting whether the second port of the next IP of the current IP is occupied when a second access request is received, so as to allocate the second access request, the method further includes: If the second access request is successfully forwarded, the port occupied by the second access request will be released.

8. A port allocation device, characterized in that, The device is installed at a designated gateway, which contains multiple IPs, each IP corresponding to multiple ports; the device includes: The request receiving module is used to receive a first access request and detect whether the first port of the current IP is occupied; wherein, the first port includes: the next port of the port previously allocated to the current IP; wherein, the current IP is the next IP of the IP corresponding to the previous request after allocation is completed; The request allocation module is used to allocate the first access request to the first port if the first port is not occupied; when a second access request is received, it checks whether the second port of the next IP of the current IP is occupied, so as to allocate the second access request; the second port is the port after the port that was last allocated to the next IP of the current IP. The port detection module is used to detect whether the next port corresponding to the first port of the current IP is occupied if the first port is occupied; if the next port of the first port is not occupied, the first access request is assigned to the next port of the first port.

9. An electronic device, characterized in that, The electronic device includes a processor and a memory, the memory storing machine-executable instructions that can be executed by the processor, the processor executing the machine-executable instructions to implement the port allocation method according to any one of claims 1 to 7.

10. A machine-readable storage medium, characterized in that, The machine-readable storage medium stores machine-executable instructions, which, when invoked and executed by a processor, cause the processor to implement the port allocation method according to any one of claims 1 to 7.