Network port resource management method and system
By intercepting the connect function to obtain system resource information and dynamically adjusting the network port range, the problems of unstable network connection and unstable system performance are solved, and the success rate of network requests and system stability are improved.
Patent Information
- Application Number
- CN202410926310.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-11
- Publication Date
- 2026-01-13
AI Technical Summary
Existing technologies cannot dynamically adjust the network port range based on system resource usage, resulting in unstable network connections and unstable system performance.
By intercepting the connect function using Hook technology, system resource information can be obtained, and the network port range can be dynamically adjusted according to the resource situation, including shortening or extending the TIME_WAIT state duration and port range. This is implemented using the Detours library in Windows systems and the LD_PRELOAD dynamic link library in Linux systems.
It automatically adjusts network port resources based on system load, improving network request success rate and system stability. It is applicable to Windows and Linux operating systems.
Smart Images

Figure CN121333918A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of network communication, in particular to a network port resource management method and system. BACKGROUND
[0002] In the case of depletion of network port resources of the operating system, the application program creation network request will fail. This is because the number of network ports of the operating system is limited, when the occupied ports are not released in time and all ports are occupied, the newly initiated network request cannot obtain available port resources, resulting in request failure. In order to solve this problem, it is usually required to set the network port range in advance by the operation and maintenance personnel when configuring the server, so as to ensure that there are enough port resources for network requests.
[0003] This way of statically setting the network port range has certain limitations. First of all, due to the uncertainty of network traffic, the statically set port range may not meet the actual demand, and the situation of high load of the operating system or insufficient network port resources may occur. Specifically, if the network port is set too large, the system will allocate a large number of ports for network connection, and when a large number of network connections are created, the system resources (such as memory and CPU) will be occupied, thereby affecting the performance and stability of the system. On the other hand, if the network port is set too small, the system cannot allocate enough port resources for all network requests under high load, resulting in network request failure.
[0004] Therefore, a technology is needed to dynamically adjust the network port range according to the system resource usage. SUMMARY
[0005] In order to overcome the problems in the prior art network port resource optimization technology, such as the inability to dynamically adjust the network port range according to the system resource usage, unstable network connection, and the inability to guarantee the stability of system performance, the present application provides a network port resource management method and system, which can guarantee efficient network connection and stable system performance.
[0006] The present application adopts the following scheme:
[0007] A network port resource management method, the steps of the method are as follows:
[0008] Step 1: Inject the function of intercepting the connect function into the target program through the Hook technology;
[0009] Step 2: Intercept the function of the connect function, that is, when the target program initiates a network request and calls the underlying connect function, intercept the connect function;
[0010] Step 3: Obtain the current system resource information;
[0011] Step 4: dynamically adjust network port resources according to current system resource information.
[0012] Further, in the Windows system, the function of intercepting the connect function is injected into the target program through the Detours library;
[0013] In the Linux system, the function of intercepting the connect function is injected into the target program through the LD_PRELOAD dynamic link library.
[0014] Further, the system resource information includes the number of remaining available network ports, the usage rate of CPU and memory.
[0015] Further, step 4 is further specified as follows: if the number of remaining available network ports is greater than 0, the underlying connect function is called to create a network connection and return;
[0016] If the number of remaining available network ports is equal to 0 and the usage rate of CPU or memory exceeds a specified threshold, the duration of the TIME_WAIT state is shortened to make the closed network connection release the port resources as soon as possible, and after the duration of the TIME_WAIT state is increased by 1 second, the underlying connect function is called to create a network connection and return;
[0017] If the number of remaining available network ports is equal to 0 and the usage rate of CPU or memory does not exceed the specified threshold, the network port range is expanded to provide more network port resources for the target program, and finally, the underlying connect function is called to create a network connection and return.
[0018] Further, in the Windows system, the duration of the TIME_WAIT state is shortened by modifying the TcpTimedWaitDelay registry parameter;
[0019] In the Linux system, the duration of the TIME_WAIT state is shortened by modifying the net.ipv4.tcp_fin_timeout kernel parameter.
[0020] A network port resource management system, the system comprising: a Hook module, an interception module, a monitoring module and an adjustment module;
[0021] The Hook module is used to inject the interception module into the target program through Hook technology;
[0022] The interception module is used to intercept the connect function when the target program initiates a network request and calls the underlying connect function, and call the monitoring module;
[0023] The monitoring module is used for acquiring current system resource information and delivering the system resource information to the adjusting module;
[0024] The adjusting module is used for dynamically adjusting network port resources according to the system resource information provided by the monitoring module.
[0025] Further, in the Windows system, the function of intercepting the connect function is injected into the target program through the Detours library;
[0026] In the Linux system, the function of intercepting the connect function is injected into the target program through the LD_PRELOAD dynamic link library.
[0027] Further, the system resource information includes the number of remaining available network ports, the usage rate of CPU and memory.
[0028] Further, the adjusting module is further specifically: if the number of remaining available network ports is greater than 0, the underlying connect function is called to create a network connection and return;
[0029] If the number of remaining available network ports is equal to 0 and the usage rate of CPU or memory exceeds a specified threshold, the length of the TIME_WAIT state is shortened, so that the network connection that has been closed releases the port resources as soon as possible, and after the length of the TIME_WAIT state is added by 1 second, the underlying connect function is called to create a network connection and return;
[0030] If the number of remaining available network ports is equal to 0 and the usage rate of CPU or memory does not exceed the specified threshold, the network port range is expanded to provide more network port resources for the target program, and finally, the underlying connect function is called to create a network connection and return.
[0031] Further, in the Windows system, the length of the TIME_WAIT state is shortened by modifying the TcpTimedWaitDelay registry parameter;
[0032] In the Linux system, the length of the TIME_WAIT state is shortened by modifying the net.ipv4.tcp_fin_timeout kernel parameter.
[0033] The present application has the following advantages:
[0034] The application dynamically adjusts network configuration parameters of an operating system through Hook technology, optimizes utilization of network port resources, improves success rate of network requests and stability of the system, and when a target program initiates a network request, through real-time acquisition of port, CPU and memory usage of the system, dynamically adjusts the length of time of a TIME_WAIT state or extends a port range, so as to ensure efficient network connection and stable system performance, the method has high automation degree, is suitable for Windows and Linux operating systems, and has wide application value. BRIEF DESCRIPTION OF DRAWINGS
[0035] Figure 1 is a flow chart of the method of the application;
[0036] Figure 2 is a structure block diagram of the system of the application. DETAILED DESCRIPTION
[0037] The application will be further described below in combination with the drawings.
[0038] Referring to Figure 1 A network port resource management method, and the method comprises the following steps:
[0039] Step 1: injecting the function of intercepting a connect function into a target program through Hook technology;
[0040] Step 2: intercepting the connect function, that is, when the target program initiates a network request and calls an underlying connect function, intercepting the connect function;
[0041] Step 3: acquiring current system resource information;
[0042] Step 4: dynamically adjusting network port resources according to the current system resource information.
[0043] The application will be further described below in combination with a specific embodiment:
[0044] A network port resource management method, and the method comprises the following steps:
[0045] Step S1, injecting the function of intercepting a connect function into a target program through Hook technology:
[0046] In a Windows system, the injection is realized through a Detours library, and in a Linux system, the injection is realized through an LD_PRELOAD dynamic link library.
[0047] Step S2, when the target program initiates a network request, an underlying connect function is called, and the connect function is intercepted.
[0048] Step S3: Obtain the number of remaining available network ports and the utilization rates of CPU and memory.
[0049] Step S4: If the number of remaining available network ports is greater than 0, call the underlying connect function to create a network connection and return.
[0050] If the number of remaining available network ports is 0 and the CPU or memory usage exceeds the specified threshold, shorten the duration of the TIME_WAIT state to allow closed network connections to release port resources as soon as possible. After waiting for the duration of the TIME_WAIT state to increase by 1 second, call the underlying connect function to create a network connection and return.
[0051] TIME_WAIT is a state in the TCP protocol that indicates a connection has been closed, but a period of time is required to ensure that any delayed packets in the network are processed correctly. In a high-concurrency server environment, if a large number of connections enter the TIME_WAIT state, it may lead to the exhaustion of available ports. Each TCP connection requires a unique four-tuple (source IP, source port, destination IP, destination port). If these ports are all occupied by the TIME_WAIT state, new connections will not be able to be established. The port exhaustion problem can be alleviated by appropriately adjusting system parameters to reduce the duration of TIME_WAIT, allowing closed connections to be released as soon as possible.
[0052] In Windows systems, the duration of the TIME_WAIT state can be shortened by modifying the TcpTimedWaitDelay registry parameter;
[0053] In Linux systems, the duration of the TIME_WAIT state can be shortened by modifying the net.ipv4.tcp_fin_timeout kernel parameter.
[0054] If the number of remaining available network ports is 0 and the CPU or memory usage does not exceed the specified threshold, the network port range is expanded to provide more network port resources for the target program. Finally, the underlying connect function is called to create a network connection and return.
[0055] In Windows systems, the network port range can be expanded by modifying the TcpNumPorts registry parameter.
[0056] In Linux systems, the network port range can be expanded by modifying the net.ipv4.ip_local_port_range kernel parameter.
[0057] See Figure 2A network port resource management system, the system comprising: a Hook module, an interception module, a monitoring module, and an adjustment module;
[0058] The Hook module is used to inject the interception module into the target program using Hook technology;
[0059] The interception module is used to intercept the connect function and call the monitoring module when the target program initiates a network request and calls the underlying connect function.
[0060] The monitoring module is used to obtain current system resource information;
[0061] The adjustment module is used to dynamically adjust network port resources based on the system resource information provided by the monitoring module.
[0062] In one embodiment of the present invention, the system resource information includes the number of remaining available network ports, CPU and memory utilization rates.
[0063] In one embodiment of the present invention, the adjustment module is further specifically configured to: if the number of remaining available network ports is greater than 0, call the underlying connect function to create a network connection and return;
[0064] If the number of remaining available network ports is 0 and the CPU or memory usage exceeds the specified threshold, shorten the duration of the TIME_WAIT state to allow closed network connections to release port resources as soon as possible. After waiting for the duration of the TIME_WAIT state to increase by 1 second, call the underlying connect function to create a network connection and return.
[0065] If the number of remaining available network ports is 0 and the CPU or memory usage does not exceed the specified threshold, the network port range is expanded to provide more network port resources for the target program. Finally, the underlying connect function is called to create a network connection and return.
[0066] The following are application scenarios encountered during the implementation of this invention:
[0067] Scenario 1: The number of remaining available ports is 0 and the operating system load is too high.
[0068] In the Windows operating system, the present invention injects an interception module into the target program through the Detours library. When the target program initiates a network connection, it calls the system's underlying connect function. The interception module intercepts the connect function and, before calling the connect function, calls the monitoring module to obtain that the number of remaining available ports in the system is 0, the CPU utilization is 90%, and the memory utilization is 60%. Since the CPU utilization exceeds the set threshold of 80%, the adjustment module modifies the TcpTimedWaitDelay registry parameter to set the duration of the TIME_WAIT state to 1 second, so that the closed network connection releases port resources after 1 second. After waiting for 2 seconds, the adjustment module calls the underlying connect function to successfully create a network connection and returns.
[0069] This example illustrates that when there are no available ports and the operating system is overloaded, the solution of this invention will release the network port resources of closed connections as soon as possible, thereby improving the success rate of establishing new network connections.
[0070] Scenario 2: The number of remaining available ports is 0 and the operating system load is not high.
[0071] In the Linux operating system, the present invention injects the interception module into the target program through the LD_PRELOAD dynamic link library. When the target program initiates a network connection, it calls the system's underlying connect function. The interception module intercepts the connect function and, before calling the connect function, calls the monitoring module to obtain that the number of remaining available ports in the system is 0, the CPU utilization is 60%, and the memory utilization is 60%. Since the CPU and memory utilization have not exceeded the set threshold of 80%, the adjustment module expands the network port range by modifying the net.ipv4.ip_local_port_range kernel parameter, enabling the system to provide more network port resources. Then, the underlying connect function is called to successfully create a network connection and return.
[0072] This example illustrates that when there are no available ports and the operating system load is low, the solution of this invention will expand the network port range of the system, enabling the system to provide more network port resources and thus increasing the success rate of establishing new network connections.
[0073] The above description is only a preferred embodiment of the present invention. All equivalent changes and modifications made within the scope of the claims of the present invention should be included in the scope of the present invention.
Claims
1. A method for network port resource management, characterized in that, The method steps are as follows: Step 1: Inject the functionality to intercept the connect function into the target program using Hook technology; Step 2: The function of intercepting the connect function is to intercept the connect function when the target program initiates a network request and calls the underlying connect function; Step 3: Obtain current system resource information; Step 4: Dynamically adjust network port resources based on the current system resource information.
2. The method for network port resource management according to claim 1, characterized in that, In Windows systems, the Detours library is used to inject the functionality of intercepting the connect function into the target program; In Linux systems, the LD_PRELOAD dynamic link library is used to inject the functionality of intercepting the connect function into the target program.
3. The method for network port resource management according to claim 1, characterized in that, The system resource information includes the number of remaining available network ports, CPU and memory usage.
4. The method for network port resource management according to claim 3, characterized in that, Step 4 is further specified as follows: If the number of remaining available network ports is greater than 0, then call the underlying connect function to create a network connection and return; If the number of remaining available network ports is 0 and the CPU or memory usage exceeds the specified threshold, shorten the duration of the TIME_WAIT state to allow closed network connections to release port resources as soon as possible. After waiting for the duration of the TIME_WAIT state to increase by 1 second, call the underlying connect function to create a network connection and return. If the number of remaining available network ports is 0 and the CPU or memory usage does not exceed the specified threshold, the network port range is expanded to provide more network port resources for the target program. Finally, the underlying connect function is called to create a network connection and return.
5. The method for network port resource management according to claim 1, characterized in that, In Windows systems, the duration of the TIME_WAIT state can be shortened by modifying the TcpTimedWaitDelay registry parameter; In Linux systems, the duration of the TIME_WAIT state can be shortened by modifying the net.ipv4.tcp_fin_timeout kernel parameter.
6. A system for managing network port resources, characterized in that, The system includes: a Hook module, an interception module, a monitoring module, and an adjustment module; The Hook module is used to inject the interception module into the target program using Hook technology; The interception module is used to intercept the connect function and call the monitoring module when the target program initiates a network request and calls the underlying connect function. The monitoring module is used to obtain the current system resource information and transmit the system resource information to the adjustment module; The adjustment module is used to dynamically adjust network port resources based on the system resource information provided by the monitoring module.
7. A network port resource management system according to claim 6, characterized in that, In Windows systems, the Detours library is used to inject the functionality of intercepting the connect function into the target program; In Linux systems, the LD_PRELOAD dynamic link library is used to inject the functionality of intercepting the connect function into the target program.
8. A network port resource management system according to claim 6, characterized in that, The system resource information includes the number of remaining available network ports, CPU and memory usage.
9. A network port resource management system according to claim 8, characterized in that, The adjustment module is further specified as follows: if the number of remaining available network ports is greater than 0, the underlying connect function is called to create a network connection and return. If the number of remaining available network ports is 0 and the CPU or memory usage exceeds the specified threshold, shorten the duration of the TIME_WAIT state to allow closed network connections to release port resources as soon as possible. After waiting for the duration of the TIME_WAIT state to increase by 1 second, call the underlying connect function to create a network connection and return. If the number of remaining available network ports is 0 and the CPU or memory usage does not exceed the specified threshold, the network port range is expanded to provide more network port resources for the target program. Finally, the underlying connect function is called to create a network connection and return.
10. A network port resource management system according to claim 6, characterized in that, In Windows systems, the duration of the TIME_WAIT state can be shortened by modifying the TcpTimedWaitDelay registry parameter; In Linux systems, the duration of the TIME_WAIT state can be shortened by modifying the net.ipv4.tcp_fin_timeout kernel parameter.