TCP long connection maintaining method and device based on dynamic heartbeat packet strategy
By dynamically calculating and adjusting the heartbeat packet transmission frequency, the connection interruption and resource waste caused by the fixed transmission frequency of the existing technology center is solved, and the stability of TCP long connection and system reliability are achieved.
Patent Information
- Application Number
- CN202411670802.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-21
- Publication Date
- 2025-02-28
AI Technical Summary
The existing technology center has a fixed frequency of packet-hopping, which cannot dynamically adapt to changes in network state, resulting in easy interruption of connections when the network fluctuates greatly, or waste of resources when the network is stable.
By obtaining the initial network status data and real-time network status data, the real-time heartbeat packet transmission frequency is calculated, and the heartbeat packet transmission frequency is adjusted in real time, the connection status is monitored in real time and retransmission or repair operations are performed.
It realizes dynamic adaptation to network state changes, avoids connection interruption and resource waste, and improves the stability of TCP long connections and system reliability.
Smart Images

Figure CN119544826A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of network communication technology, and in particular to a TCP long connection maintaining method and device based on a dynamic heartbeat packet strategy. Background Art
[0002] At present, in network communications, the traditional TCP long connection maintenance method ensures that the connection is not interrupted by sending heartbeat packets at fixed intervals. However, the actual network environment is complex and changeable, and the server needs to effectively respond to fluctuations in network status, especially under high load conditions. It is particularly urgent to reduce unnecessary resource consumption and respond to network changes in a timely manner to ensure the stability and efficiency of the connection.
[0003] In one prior art, a longer fixed heartbeat interval is set to reduce the frequency of sending heartbeat packets in an attempt to save system resources. However, this method cannot adjust the frequency of sending heartbeat packets in time when the network status changes dramatically, resulting in easy connection interruption or high delay, and cannot meet high real-time requirements.
[0004] In summary, the heartbeat packet sending frequency in the prior art is fixed and cannot dynamically adapt to changes in network status, which easily leads to connection interruption when the network fluctuates greatly or waste of resources when the network is stable. Summary of the invention
[0005] The present invention provides a TCP long connection maintenance method, device, electronic device and storage medium based on a dynamic heartbeat packet strategy, so as to achieve dynamic adaptation to changes in network status and solve the problems of connection interruption when the network fluctuates greatly or resource waste when the network is stable in the TCP connection.
[0006] In a first aspect, in order to solve the above technical problems, the present invention provides a TCP long connection maintenance method based on a dynamic heartbeat packet strategy, comprising:
[0007] Acquire initial network status data, constraint conditions, and real-time network status data, wherein the initial network status data includes an initial heartbeat packet sending frequency, multiple historical network fluctuation coefficients, and a communication success rate;
[0008] Calculating a frequency target function value according to the initial network state data;
[0009] According to the frequency objective function value, the real-time network status data and the constraint condition, the real-time heartbeat packet sending frequency is calculated;
[0010] Based on the real-time heartbeat packet sending frequency, adjusting the heartbeat packet sending frequency;
[0011] Monitor the sending and receiving status of heartbeat packets in real time and obtain real-time monitoring data, which includes the heartbeat packet sending success rate, receiving confirmation rate, packet loss rate and network delay;
[0012] According to the real-time monitoring data, determine the connection status and perform retransmission or repair operations, and record the total number of repair operations and the total number of retransmission operations;
[0013] Connection maintenance feedback data is generated according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations, and is used as input for the next connection maintenance cycle.
[0014] In one implementation, the method of obtaining the initial network status data includes:
[0015] Acquire initial network status data according to the historical network status data recorded by the user;
[0016] The feedback data from the previous round of connection maintenance is used as the initial network status data.
[0017] In one implementation, the calculating, according to the initial network state data, a frequency objective function value includes:
[0018] The calculation formula of the frequency objective function value is:
[0019]
[0020] Among them, f(S,R) is the frequency objective function value, R is the communication success rate, S1, S2, ..., S n is the historical network fluctuation coefficient, n is the number of historical network fluctuation coefficients, f init is the frequency of sending the initial heartbeat packet, and a, b, and c are weight proportional factors.
[0021] In one implementation, the calculating the real-time heartbeat packet sending frequency according to the frequency objective function value, the real-time network status data and the constraint condition includes:
[0022] The real-time network status data includes a real-time network fluctuation coefficient and a real-time communication success rate;
[0023] The constraint conditions include a minimum frequency and a maximum frequency;
[0024] When the real-time network fluctuation coefficient is less than 0 or the real-time communication success rate is greater than 1, the real-time heartbeat packet sending frequency is calculated as follows:
[0025]
[0026] Among them, f rtis the frequency of sending real-time heartbeat packets, a, b, c are weight proportional factors;
[0027] When the real-time network fluctuation coefficient is greater than 0 or the real-time communication success rate is less than 0, the real-time heartbeat packet sending frequency is calculated as follows:
[0028]
[0029] Among them, f rt is the frequency of sending real-time heartbeat packets, f1 is the minimum frequency of the constraint condition, and f2 is the maximum frequency of the constraint condition.
[0030] In one implementation, judging the connection status and performing a retransmission or repair operation according to the real-time monitoring data, and recording a total number of repair operations and a total number of retransmission operations, includes:
[0031] When no reply is received after the heartbeat packet is sent, a retransmission operation is performed; wherein, the number of retransmission operations after each heartbeat packet is sent cannot exceed the preset retransmission number threshold;
[0032] Record the retransmission operation, and take the sum of all retransmission times in this cycle as the total number of retransmission operations;
[0033] When it is determined that an error event occurs or the connection is disconnected, a repair operation is performed;
[0034] Record the repair operations, and take the sum of all repair times in this cycle as the total number of repair operations.
[0035] In one embodiment, when it is determined that an error event occurs or the connection is disconnected, performing a repair operation includes:
[0036] When the number of retransmission operations exceeds a preset retransmission number threshold, it is determined that an error event occurs and a repair operation is performed;
[0037] When the network delay is higher than a preset threshold or the packet loss rate is greater than a preset threshold, it is determined that a connection is disconnected and a repair operation is performed;
[0038] The repair operation includes re-establishing a connection, switching to a backup link, or increasing the frequency of sending heartbeat packets.
[0039] In one implementation, generating connection maintenance feedback data according to the real-time monitoring data, the total number of repair operations, and the total number of retransmission operations includes:
[0040] Collect real-time monitoring data, total repair operation times, and total retransmission operation times within the current maintenance cycle as periodic maintenance data;
[0041] Analyze the periodic maintenance data, and generate connection maintenance feedback data according to the data analysis result, wherein the connection maintenance feedback data includes a frequency adjustment factor and an objective function weight update parameter;
[0042] The connection maintenance feedback data is stored in a structured manner so as to be used in the next round of connection maintenance.
[0043] In a second aspect, the present invention provides a TCP long connection maintenance device based on a dynamic heartbeat packet strategy, comprising:
[0044] An initial data acquisition module is used to acquire initial network status data, constraint conditions and real-time network status data, wherein the initial network status data includes an initial heartbeat packet sending frequency, multiple historical network fluctuation coefficients and a communication success rate;
[0045] An objective function calculation module, used to calculate a frequency objective function value according to the initial network state data;
[0046] A real-time frequency calculation module, used for calculating the real-time heartbeat packet sending frequency according to the frequency objective function value, real-time network status data and constraint conditions;
[0047] A sending frequency adjustment module, which adjusts the sending frequency of the heartbeat packet based on the real-time heartbeat packet sending frequency;
[0048] A real-time data monitoring module is used to monitor the sending and receiving status of heartbeat packets in real time and obtain real-time monitoring data, which includes the heartbeat packet sending success rate, receiving confirmation rate, packet loss rate and network delay;
[0049] A connection status repair module, used to determine the connection status and perform retransmission or repair operations according to the real-time monitoring data, and record the total number of repair operations and the total number of retransmission operations;
[0050] The feedback data generating module is used to generate connection maintenance feedback data according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations, and use it as input for the next connection maintenance cycle.
[0051] In a third aspect, the present invention also provides an electronic device comprising a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein when the processor executes the computer program, the method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy as described above is implemented.
[0052] In a fourth aspect, the present invention also provides a computer-readable storage medium, which includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute any one of the above-mentioned TCP long connection maintenance methods based on dynamic heartbeat packet strategy.
[0053] Compared with the prior art, the present invention has the following beneficial effects:
[0054] The invention discloses a TCP long connection maintenance method based on a dynamic heartbeat packet strategy, comprising obtaining initial network state data, constraint conditions and real-time network state data, wherein the initial network state data comprises an initial heartbeat packet sending frequency, a plurality of historical network fluctuation coefficients and a communication success rate; calculating a frequency target function value according to the initial network state data; calculating a real-time heartbeat packet sending frequency according to the frequency target function value, the real-time network state data and the constraint conditions; adjusting the heartbeat packet sending frequency based on the real-time heartbeat packet sending frequency; monitoring the sending and receiving states of the heartbeat packets in real time, and obtaining real-time monitoring data, wherein the real-time monitoring data comprises a heartbeat packet sending success rate, a receiving confirmation rate, a packet loss rate and a network delay; judging a connection state and performing a retransmission or repair operation according to the real-time monitoring data, and recording and obtaining a total number of repair operations and a total number of retransmission operations; generating connection maintenance feedback data according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations, and using the data as input for the next connection maintenance cycle.
[0055] The present invention adopts a dynamic heartbeat packet strategy, adaptively adjusts the heartbeat packet sending frequency, monitors the network status and connection quality in real time, accurately predicts and repairs connection problems, and solves the problems of large resource consumption and unstable connection in the traditional fixed heartbeat interval method. While ensuring the stability of TCP long connection, it effectively reduces network overhead and significantly improves the reliability and adaptability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Figure 1 It is a flow chart of a method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy provided by the first embodiment of the present invention;
[0057] Figure 2 It is a schematic diagram of the structure of a TCP long connection maintaining device based on a dynamic heartbeat packet strategy provided by the second embodiment of the present invention. DETAILED DESCRIPTION
[0058] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0059] Reference Figure 1 The first embodiment of the present invention provides a method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy, comprising the following steps:
[0060] S100, obtaining initial network status data, constraint conditions and real-time network status data, wherein the initial network status data includes an initial heartbeat packet sending frequency, a plurality of historical network fluctuation coefficients and a communication success rate; S200, calculating a frequency target function value according to the initial network status data;
[0061] S300, calculating a real-time heartbeat packet sending frequency according to the frequency objective function value, the real-time network status data and the constraint condition;
[0062] S400, adjusting the sending frequency of the heartbeat packets based on the real-time heartbeat packet sending frequency;
[0063] S500, monitoring the sending and receiving status of the heartbeat packet in real time, and obtaining real-time monitoring data, wherein the real-time monitoring data includes a heartbeat packet sending success rate, a receiving confirmation rate, a packet loss rate, and a network delay;
[0064] S600, judging the connection status and performing a retransmission or repair operation according to the real-time monitoring data, and recording a total number of repair operations and a total number of retransmission operations;
[0065] S700: Generate connection maintenance feedback data according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations, and use it as input for the next connection maintenance cycle.
[0066] In step S100, the system obtains initial network status data, constraints, and real-time network status data through a series of processes. This step is the basis of the entire dynamic heartbeat packet strategy, which ensures that the system can make decisions based on accurate and comprehensive network information in subsequent operations. First, the system extracts initial network status data from a preset data source or historical records. This process includes collecting relevant network performance data from previous connection records and combining the default values set by the system or user-defined parameters to form the initial network status.
[0067] In an optional implementation, the initial network status data includes an initial heartbeat packet sending frequency, a plurality of historical network fluctuation coefficients and a communication success rate, wherein:
[0068] Initial heartbeat packet sending frequency: At the beginning of the connection, the system sets a default heartbeat packet sending frequency, which is used to maintain the basic communication status of the TCP long connection. This frequency is based on past network status experience or system default values, and the purpose is to ensure that the heartbeat packet can be sent in time at the beginning of the connection, thereby maintaining the stability of the connection;
[0069] The historical network fluctuation coefficient indicates the stability of the network in the past, which is calculated by monitoring network delay, packet loss rate and other parameters. The system collects fluctuation data in historical records, counts and calculates the network fluctuation coefficient. This coefficient can indicate whether the network environment in the past was relatively stable or had large fluctuations.
[0070] The communication success rate is the ratio of the number of successful data packet transmissions in the network communication to the total number of data packet transmissions in the historical records. It reflects the reliability of the network connection in history, that is, the success or failure of past communications. The higher the communication success rate, the better the transmission stability of the network.
[0071] In an optional implementation, the method of obtaining the initial network status data includes:
[0072] Obtaining initial network status data based on historical network status data recorded by users: The system extracts information from historical network status data recorded by users as initial network status data. These historical data include information such as the frequency of heartbeat packets sent in the past, network fluctuation coefficient, and communication success rate. By analyzing these data, the system can evaluate the overall situation of the past network, thereby setting a suitable initial network status and providing a reference for subsequent connection maintenance.
[0073] In addition to using the feedback data from the previous round of connection maintenance as the initial network status data, the system can also directly use the feedback data from the previous round of connection maintenance as the initial network status for this round. This means that the system will infer the initial value of the current network status based on the connection status of the previous round, including its success rate, fluctuations, and the heartbeat frequency at that time. This method is particularly suitable for scenarios where the network environment does not change much, which can reduce the initialization time and enable the system to quickly enter the next round of heartbeat packet adjustment and connection maintenance process.
[0074] In step S200, the system calculates the frequency objective function value based on the initial network status data previously obtained. This calculation process is the core link in the method of the present invention, which directly affects the adjustment of the subsequent heartbeat packet sending frequency. In this calculation process, the system will comprehensively consider multiple network fluctuation coefficients recorded in the historical network status. These coefficients reflect the stability or fluctuation of the network in different time periods in the past. At the same time, the communication success rate, as an important indicator of network transmission reliability, will also have an important impact on the objective function value. In addition, the system will balance the contribution of different data to the frequency objective function through set weight factors, so that the focus of the calculation process can be adjusted according to different network environments.
[0075] In an optional implementation, the calculating, according to the initial network state data, a frequency objective function value includes:
[0076] The calculation formula of the frequency objective function value is:
[0077]
[0078] Among them, f(S,R) is the frequency objective function value, R is the communication success rate, S1, S2, ..., S n is the historical network volatility coefficient, i is the number of historical network volatility coefficients, and f init is the initial heartbeat packet sending frequency, a, b, c are weight proportional factors; where,
[0079] The frequency objective function value is the final calculated objective function value, which represents a heartbeat packet sending frequency suitable for the current network status. This value combines factors such as network fluctuations, communication success rate, and initial frequency, and is used to determine the heartbeat packet sending frequency under current conditions.
[0080] The communication success rate is the ratio of the number of successfully transmitted data packets to the total number of transmitted data packets in the past period of time. It reflects the historical reliability of the network and is used to judge the stability of the connection. The higher the value, the smoother the data transmission.
[0081] The historical network fluctuation coefficient is a fluctuation coefficient calculated based on historical network data, which indicates the network fluctuation in different time periods in the past. The fluctuation coefficient is calculated based on indicators such as delay and packet loss rate, reflecting the stability or change trend of the network environment.
[0082] The initial heartbeat packet sending frequency is the default heartbeat packet sending frequency set at the beginning of the connection. This frequency is used to maintain the basic TCP long connection. As the basic frequency of the objective function, it adapts to the stability requirements before the actual connection state.
[0083] The weight scaling factor is a scaling factor used to adjust the contribution of each part in the formula to the objective function value.
[0084] In an optional implementation, the historical network fluctuation coefficients S1, S2, ..., S n The calculation method based on historical connection data is:
[0085] S i =w1·L i +w2·P i +w3·R i
[0086] Among them, S i They are S1, S2, ..., S n , which is the historical network volatility coefficient, w1, w2, and w3 are weight ratio coefficients.
[0087] Among them, L i Indicates network latency, which refers to the time it takes for a data packet to be sent and received. The greater the network latency, the more likely it is that there is congestion or other performance issues in the network, so it plays a vital role in network fluctuations;
[0088] P i Represents the packet loss rate, which is the ratio of data packets lost during transmission to the total number of packets sent. The higher the packet loss rate, the worse the network reliability. The lost data in communication will affect the stability of the overall connection. The packet loss rate is another key indicator in the calculation of the historical network fluctuation coefficient, which is used to evaluate the reliability of network transmission.
[0089] R i Represents the number of retransmissions, that is, the number of packets that need to be retransmitted during this time period because of unsuccessful transmission. The higher the number of retransmissions, the worse the network condition, that is, the initial transmission failed due to signal interference, congestion, etc., which led to the secondary transmission. This parameter further enriches the description of network fluctuations by reflecting the effective transmission rate of the network.
[0090] By assigning weight coefficients w1, w2, and w3 to each network indicator, the system can flexibly adjust the contribution of different network parameters to the fluctuation coefficient according to actual needs. For example, if the delay changes significantly in a certain network environment, the system can increase w1 to increase the influence of delay in the fluctuation coefficient; conversely, if the packet loss rate is the main influencing factor, increasing w2 can better reflect this feature. In this way, the system can adjust the weight ratio according to different application scenarios or network requirements, thereby dynamically optimizing the calculation of historical network fluctuation coefficients.
[0091] In step S300, according to the initial network status data, constraints and real-time network status data acquired by the system, the frequency objective function value is first calculated based on historical data. When the real-time network fluctuation coefficient is less than 0 or the real-time communication success rate is greater than 1, the real-time heartbeat packet sending frequency is obtained using a calculation formula based on a weight proportional factor; when the real-time network fluctuation coefficient is greater than 0 or the real-time communication success rate is less than 0, the maximum and minimum frequency averages are used as the real-time heartbeat packet sending frequency, and finally the heartbeat packet sending interval is dynamically adjusted based on the calculated frequency.
[0092] In an optional implementation, the calculating the real-time heartbeat packet sending frequency according to the frequency objective function value, the real-time network status data and the constraint condition includes:
[0093] The real-time network status data includes the real-time network fluctuation coefficient and the real-time communication success rate, wherein:
[0094] The real-time network fluctuation coefficient is an instant coefficient based on the current network status, reflecting the stability of the current network environment. The calculation of the network fluctuation coefficient is based on parameters such as real-time network delay, packet loss rate, and number of packet retransmissions. When the fluctuation coefficient is high, it means that the current network environment is unstable and the system needs to increase the frequency of heartbeat packets to ensure the reliability of the connection;
[0095] The real-time communication success rate refers to the ratio of successfully transmitted data packets to the total number of data packets in real-time data transmission. A high communication success rate means a high network transmission success rate and a more stable connection. You can consider reducing the frequency of sending heartbeat packets appropriately. On the contrary, if the communication success rate is low, it indicates that the network has unstable transmission. At this time, you can increase the frequency of sending heartbeat packets to enhance connection monitoring.
[0096] The constraints include a minimum frequency and a maximum frequency, where:
[0097] The minimum frequency is the lowest heartbeat packet sending frequency allowed by the system. It is used to prevent the connection status from being difficult to monitor due to a low frequency.
[0098] The maximum frequency is the highest heartbeat packet sending frequency allowed by the system. The purpose is to avoid resource waste and unnecessary network overhead caused by too high a frequency.
[0099] In an optional implementation, the calculation method of the real-time heartbeat packet sending frequency includes:
[0100] When the real-time network fluctuation coefficient is less than 0 or the real-time communication success rate is greater than 1, the real-time heartbeat packet sending frequency is calculated as follows:
[0101]
[0102] Among them, f rt is the frequency of sending real-time heartbeat packets, a, b, c are weight proportional factors;
[0103] When the real-time network fluctuation coefficient is greater than 0 or the real-time communication success rate is less than 0, the real-time heartbeat packet sending frequency is calculated as follows:
[0104]
[0105] Among them, f rt is the frequency of sending real-time heartbeat packets, f1 is the minimum frequency of the constraint condition, and f2 is the maximum frequency of the constraint condition.
[0106] In step S400, based on the real-time heartbeat packet sending frequency, adjusting the heartbeat packet sending frequency includes:
[0107] The system updates the actual sending frequency of the current heartbeat packet based on the calculated real-time heartbeat packet sending frequency. This adjustment process updates the old sending frequency to the latest real-time frequency, allowing the system to flexibly respond to instant changes in the network.
[0108] For example, when the real-time heartbeat frequency increases compared to before, it means that the network environment has become unstable and the system needs to increase the frequency of sending heartbeat packets to strengthen connection monitoring; when the real-time frequency decreases compared to before, it indicates that the network environment is relatively stable, and the system can reduce the sending frequency to reduce resource consumption.
[0109] Preferably, in some cases, the system can adopt a smooth adjustment method, that is, gradually adjust the sending frequency instead of changing it immediately, to avoid resource fluctuations caused by sudden frequency changes. This can be achieved by setting the adjustment step size, so that the heartbeat packet sending frequency transitions more smoothly, which is suitable for scenarios that are sensitive to resource consumption.
[0110] In step S500, the sending and receiving status of the heartbeat packet is monitored in real time to obtain real-time monitoring data, which includes the heartbeat packet sending success rate, receiving confirmation rate, packet loss rate and network delay, wherein:
[0111] The heartbeat packet sending success rate refers to the proportion of heartbeat packets sent by the system that successfully reach the receiving end. The higher the sending success rate, the smoother the current network conditions; if the success rate is lower, it means that the network is unstable and the system needs to increase the heartbeat packet frequency to enhance connection monitoring;
[0112] The reception confirmation rate refers to the proportion of confirmation replies to heartbeat packets received by the system, reflecting the reliability of feedback from the receiving end. If the confirmation rate decreases, it means that there is a backhaul problem in the network, and encryption or adjustment of the heartbeat frequency is required;
[0113] The packet loss rate is the proportion of data packets lost during the transmission of heartbeat packets. A high packet loss rate indicates that there is a lot of data loss in the network, and the system needs to increase the frequency of heartbeat packet transmission to ensure a stable connection;
[0114] Network latency refers to the time interval from when a heartbeat packet is sent to when it is received for confirmation. Long latency indicates that the network transmission speed is blocked or congested. The system can dynamically adjust the frequency of heartbeat packet transmission according to the latency changes to adapt to the current network bandwidth.
[0115] In step S600, the connection status is determined according to the real-time monitoring data, and a retransmission or repair operation is performed, and the total number of repair operations and the total number of retransmission operations are recorded.
[0116] Among them, if only the transmission of a heartbeat packet fails (such as no confirmation from the receiving end), the system will immediately retransmit the heartbeat packet to ensure timely monitoring of the connection status. The retransmission operation is suitable for slight and short-term network fluctuations; when it is determined that an error event has occurred or the connection is disconnected, a repair operation is performed to ensure that the system can return to a stable state.
[0117] In an optional implementation, when the system does not receive a confirmation reply from the receiving end after sending a heartbeat packet, it is considered that there is a risk of a short-term interruption in the connection, and the system will immediately perform a retransmission operation, including:
[0118] The number of retransmission operations after each heartbeat packet is sent does not exceed the preset retransmission threshold, that is, during the transmission of a single heartbeat packet, the system performs a maximum of a certain number of retransmissions to ensure the validity of the connection and avoid resource waste;
[0119] The system records each retransmission operation, and at the end of this cycle, accumulates all the retransmission times in the cycle to obtain the total number of retransmission operations.
[0120] In an optional implementation, when it is determined that an error event occurs or the connection is disconnected, performing a repair operation includes:
[0121] When the number of retransmissions of a heartbeat packet exceeds the preset retransmission threshold, the system determines that an error event has occurred, which means that there is a serious connection problem in the current network environment and the system needs to take repair measures immediately;
[0122] When the system detects that the network delay is higher than the preset threshold, or the packet loss rate exceeds the preset threshold, the system considers that the connection has been interrupted and triggers the repair operation.
[0123] In an optional implementation, the repair operation includes:
[0124] Reestablish connection: terminate the current unstable connection and try to reestablish the TCP connection to resume normal data transmission;
[0125] Switch to backup link: If the system supports multiple links, it will automatically switch to the backup link to ensure uninterrupted communication;
[0126] Increase the frequency of sending heartbeat packets: When the network is unstable, the system chooses to temporarily increase the frequency of sending heartbeat packets to improve the monitoring frequency and response speed of the connection.
[0127] In step S700, connection maintenance feedback data is generated according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations, and is used as input for the next connection maintenance cycle to provide an optimization basis for the next round of connection maintenance.
[0128] In an optional implementation, generating connection maintenance feedback data according to the real-time monitoring data, the total number of repair operations, and the total number of retransmission operations includes:
[0129] Collect periodic maintenance data. During the current maintenance cycle, the system integrates the collected real-time monitoring data, total repair operation times, and total retransmission operation times into periodic maintenance data.
[0130] The system analyzes the periodic maintenance data to identify the main problems and bottlenecks in the connection maintenance process. Data analysis will help the system determine the fluctuation characteristics of the network environment during this period and the adaptability of the system response, such as the effectiveness of retransmission and repair operations;
[0131] Based on the analysis results of the periodic maintenance data, the system generates connection maintenance feedback data including frequency adjustment factors, objective function weight update parameters, and structured saved feedback data.
[0132] Among them, the frequency adjustment factor is used to guide the adjustment of the heartbeat packet sending frequency in the next cycle to adapt to changes in network conditions. The frequency adjustment factor increases or decreases based on the number of retransmission operations and repair operations to ensure that the system responds quickly to network conditions; the objective function weight update parameter is used to adjust the weight in the objective function to optimize the calculation of the heartbeat packet sending frequency. Weight update helps the system to more accurately balance network fluctuations and resource overhead in future cycles. The system stores the generated connection maintenance feedback data in a structured manner so that it can be used as input data in the next round of connection maintenance cycle. Through structured storage, the system can quickly call previous maintenance results to guide the adjustment of the heartbeat packet sending frequency and the optimization of connection maintenance strategies in the next cycle.
[0133] In order to facilitate the understanding of the present invention, some preferred embodiments of the present invention are further described below.
[0134] In one implementation, the TCP long connection maintenance method based on the dynamic heartbeat packet strategy of the present invention can extract initial network status data from the historical network status data recorded by the user through the initial data acquisition step. This method is suitable for scenarios where the network environment is relatively stable, such as office networks or home networks. The system can directly use past data as the initial condition for the connection, thereby reducing the initialization time. At the same time, when the network state changes significantly, the initial data acquisition step can also use the feedback data from the previous round of connection maintenance to dynamically adjust the initial heartbeat packet sending frequency, thereby improving the system's adaptability to environmental changes.
[0135] In another implementation, the real-time data can also be dynamically calculated through the objective function calculation step to further optimize the setting of the heartbeat packet sending frequency. For example, when calculating the frequency objective function value, the objective function calculation step can adjust the weight factors a, b, and c according to different network states and application requirements to adapt to different network fluctuation coefficients and communication success rate requirements. This implementation method is particularly suitable for network scenarios with large fluctuations in communication requirements, such as video calls, real-time games, etc. In frequent network fluctuations, the system can flexibly adjust the heartbeat frequency to ensure the stability of the connection and the reliability of data transmission.
[0136] The working process of the present invention is described below by taking a more common scenario as an example. In an application scenario, the internal server of a certain enterprise needs to maintain a long TCP connection with the remote client in order to transmit data in real time and provide remote services. Such application scenarios have strict requirements on the stability and reliability of the connection, especially when the real-time performance of data transmission is crucial, such as video conferencing, remote collaboration or real-time data monitoring. In order to ensure the stability of the connection under the conditions of complex and changeable network environment, the system sets the initial sending frequency of the heartbeat packet through the initial data acquisition step during the connection initialization phase, and uses the past network fluctuations and communication success rate as a reference. This initial setting is formulated based on historical data, which can effectively improve the adaptability of the system in the initial stage, so that it can quickly enter the connection maintenance and dynamic monitoring stage. In this scenario, the initial heartbeat packet sending frequency set by the system is 10 seconds / time, the communication success rate is 98%, and the average value of the historical network fluctuation coefficient is 0.02. These initial parameters will be used to calculate the frequency objective function value of the system in the initial stage of connection.
[0137] Known data:
[0138] parameter Initial Value Real-time monitoring value (within maintenance period) Heartbeat packet sending frequency (seconds / time) 10 5 Communication success rate R 98% 92% Network volatility coefficient set S 0.02 0.05 Number of retransmission operations 0 2 Repair operations 0 1
[0139] The initial heartbeat packet sending frequency is:
[0140] f init(S,R)=0.5×0.02+0.3×0.98+0.2×10=0.01+0.294+2=2.304
[0141] The obtained initial frequency objective function value of 2.304 will be used as the reference frequency of the system in the initial state, ensuring that the system can maintain the connection with a lower heartbeat packet sending frequency when the network state is relatively stable, which can save resources and maintain the reliability of communication.
[0142] As the connection continues, the real-time data monitoring step continuously collects data such as the network fluctuation coefficient and communication success rate, and passes these real-time data to the real-time frequency calculation step to dynamically calculate the heartbeat packet sending frequency.
[0143] Under the current conditions, the system substitutes b=0.3, c=0.2, and a=0.5 into the calculation to obtain the real-time heartbeat packet sending frequency:
[0144] f rt =(0.3+0.2) / 0.5=1.0
[0145] This value indicates that the frequency of sending heartbeat packets needs to be increased to 5 seconds / time, so that the system can monitor the connection status more frequently and respond to fluctuating network environments in a timely manner. This adjustment ensures that when the connection status is unstable, the system can detect potential problems in time and avoid connection interruption.
[0146] The real-time network fluctuation coefficient increased to 0.05, indicating that the stability of the current network environment has decreased;
[0147] During this maintenance cycle, due to real-time monitoring that the heartbeat packet failed to successfully receive a confirmation reply, the system triggered two retransmission operations to try to resend the heartbeat packet. Under the threshold conditions set by the system, when the number of retransmissions of the heartbeat packet reaches 3 times, the system determines that the connection status is abnormal and decides to perform a repair operation. Based on the current network conditions, the system chooses to switch to the backup link to ensure the continuity of the connection. After the backup link is started, the system's real-time data monitoring module once again updated the communication success rate and network fluctuation coefficient. The results show that the communication success rate has recovered to 97% and the network fluctuation coefficient has recovered to 0.03, indicating that the system has successfully recovered to a stable connection state.
[0148] After the maintenance cycle is over, the system will record the number of retransmissions and repair operations in this cycle and summarize them into feedback data. The specific feedback data content is as follows:
[0149] index value Total number of retransmission operations 2 Total repair operations 1 Real-time heartbeat packet sending frequency 5 seconds / time Feedback frequency adjustment factor 1.2 times Objective function weight update parameters The R weight is updated to 0.6
[0150] In the feedback data, the system sets the frequency adjustment factor to 1.2 times for real-time monitoring, which is used to prioritize the frequency of heartbeat packet transmission in the next cycle, so as to increase the monitoring frequency in a more unstable network environment. In addition, in order to enhance the response to transmission reliability, the system updates the communication success rate weight in the objective function to 0.6, so as to more sensitively reflect the transmission stability problem in future cycles.
[0151] In the next maintenance cycle, the system uses the feedback data generated in the previous cycle as input and applies it to the setting and dynamic adjustment of initial parameters. Specifically, the frequency adjustment factor of 1.2 times will affect the frequency of sending new heartbeat packets, making it higher than the initial value, thereby further enhancing the ability to adapt to fluctuating environments. At the same time, the weight update parameters of the objective function will make the system more sensitive to changes in the communication success rate when calculating the frequency objective function value, allowing the system to prioritize the reliability of transmission when the network is unstable. Through the application of feedback data, the system can adaptively adjust the connection maintenance strategy, effectively improving the stability and resource utilization efficiency of long-term TCP connections, and providing reliable technical support for remote communications and real-time services.
[0152] In summary, the present invention provides a method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy, which ensures the stability and resource utilization efficiency of a TCP long connection by monitoring the network status in real time, dynamically adjusting the heartbeat packet sending frequency, performing necessary retransmission and repair operations, and generating connection maintenance feedback data. The method solves the problems of large resource consumption and unstable connection in the traditional fixed heartbeat packet strategy, and enables the system to flexibly adapt to the real-time network environment. Compared with the prior art, the present invention can not only effectively reduce network overhead, but also improve the response speed and reliability of connection maintenance, and is particularly suitable for application scenarios that require a stable connection to be maintained for a long time. The present invention provides strong support for the performance optimization of network connections through intelligent frequency adjustment and fault repair mechanisms, and provides reliable technical guarantees for real-time data transmission and efficient communication.
[0153] Reference Figure 2 The second embodiment of the present invention provides a TCP long connection maintenance device based on a dynamic heartbeat packet strategy, comprising:
[0154] An initial data acquisition module is used to acquire initial network status data, constraint conditions and real-time network status data, wherein the initial network status data includes an initial heartbeat packet sending frequency, multiple historical network fluctuation coefficients and a communication success rate;
[0155] An objective function calculation module, used to calculate a frequency objective function value according to the initial network state data;
[0156] A real-time frequency calculation module, used for calculating the real-time heartbeat packet sending frequency according to the frequency objective function value, real-time network status data and constraint conditions;
[0157] A sending frequency adjustment module, which adjusts the sending frequency of the heartbeat packet based on the real-time heartbeat packet sending frequency;
[0158] A real-time data monitoring module is used to monitor the sending and receiving status of heartbeat packets in real time and obtain real-time monitoring data, which includes the heartbeat packet sending success rate, receiving confirmation rate, packet loss rate and network delay;
[0159] A connection status repair module, used to determine the connection status and perform retransmission or repair operations according to the real-time monitoring data, and record the total number of repair operations and the total number of retransmission operations;
[0160] The feedback data generating module is used to generate connection maintenance feedback data according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations, and use it as input for the next connection maintenance cycle.
[0161] Preferably, the obtaining of initial network status data further comprises:
[0162] The historical network status data recorded by the user or the feedback data of the previous round of connection maintenance is used as the initial network status data to improve the accuracy and adaptability of the initial network status data.
[0163] It should be noted that a TCP long connection maintenance device based on a dynamic heartbeat packet strategy provided in an embodiment of the present invention is used to execute all the process steps of a TCP long connection maintenance method based on a dynamic heartbeat packet strategy in the above embodiment. The working principles and beneficial effects of the two correspond one to one, and therefore will not be repeated here.
[0164] The embodiment of the present invention also provides an electronic device. The electronic device includes: a processor, a memory, and a computer program stored in the memory and executable on the processor, such as a TCP long connection maintenance program based on a dynamic heartbeat packet strategy. When the processor executes the computer program, the steps in the above-mentioned embodiments of the TCP long connection maintenance method based on a dynamic heartbeat packet strategy are implemented, such as Figure 1 Alternatively, when the processor executes the computer program, the functions of the modules / units in the above-mentioned device embodiments are implemented, such as the sending frequency adjustment module.
[0165] Exemplarily, the computer program may be divided into one or more modules / units, which are stored in the memory and executed by the processor to implement the present invention. The one or more modules / units may be a series of computer program instruction segments capable of implementing specific functions, which are used to describe the execution process of the computer program in the electronic device.
[0166] The electronic device may be a computing device such as a desktop computer, a notebook, a PDA, and a smart tablet. The electronic device may include, but is not limited to, a processor and a memory. Those skilled in the art will appreciate that the above components are merely examples of electronic devices and do not constitute a limitation on the electronic device. The electronic device may include more or fewer components than the above components, or may combine certain components, or different components. For example, the electronic device may also include input and output devices, network access devices, buses, etc.
[0167] The processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc. The processor is the control center of the electronic device, and uses various interfaces and lines to connect various parts of the entire electronic device.
[0168] The memory can be used to store the computer program and / or module, and the processor realizes various functions of the electronic device by running or executing the computer program and / or module stored in the memory, and calling the data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area can store data created according to the use of the mobile phone (such as audio data, a phone book, etc.), etc. In addition, the memory can include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0169] Wherein, if the module / unit integrated in the electronic device is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Wherein, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0170] It should be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the accompanying drawings of the device embodiments provided by the present invention, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines. A person of ordinary skill in the art may understand and implement it without paying any creative effort.
[0171] The specific embodiments described above further illustrate the purpose, technical solutions and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. It is particularly pointed out that for those skilled in the art, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the scope of protection of the present invention.
Claims
1. A TCP long connection maintenance method based on dynamic heartbeat packet strategy, characterized in that: Executed by a computer, including: Acquire initial network status data, constraint conditions, and real-time network status data, wherein the initial network status data includes an initial heartbeat packet sending frequency, multiple historical network fluctuation coefficients, and a communication success rate; Calculating a frequency target function value according to the initial network state data; According to the frequency objective function value, the real-time network status data and the constraint condition, the real-time heartbeat packet sending frequency is calculated; Based on the real-time heartbeat packet sending frequency, adjusting the heartbeat packet sending frequency; Monitor the sending and receiving status of heartbeat packets in real time and obtain real-time monitoring data, which includes the heartbeat packet sending success rate, receiving confirmation rate, packet loss rate and network delay; According to the real-time monitoring data, determine the connection status and perform retransmission or repair operations, and record the total number of repair operations and the total number of retransmission operations; Connection maintenance feedback data is generated according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations, and is used as input for the next connection maintenance cycle.
2. The method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy according to claim 1, characterized in that: The method of obtaining the initial network status data includes: Acquire initial network status data according to the historical network status data recorded by the user; The feedback data from the previous round of connection maintenance is used as the initial network status data.
3. The method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy according to claim 1, characterized in that: The step of calculating the frequency target function value according to the initial network state data includes: The calculation formula of the frequency objective function value is: Among them, f(S,R) is the frequency objective function value, R is the communication success rate, S1, S2, ..., S n is the historical network fluctuation coefficient, n is the number of historical network fluctuation coefficients, f init is the frequency of sending the initial heartbeat packet, and a, b, and c are weight proportional factors.
4. The method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy according to claim 1, characterized in that: The step of calculating the real-time heartbeat packet sending frequency according to the frequency objective function value, the real-time network status data and the constraint condition comprises: The real-time network status data includes a real-time network fluctuation coefficient and a real-time communication success rate; The constraint conditions include a minimum frequency and a maximum frequency; When the real-time network fluctuation coefficient is less than 0 or the real-time communication success rate is greater than 1, the real-time heartbeat packet sending frequency is calculated as follows: Among them, f rt is the frequency of sending real-time heartbeat packets, a, b, c are weight proportional factors; When the real-time network fluctuation coefficient is greater than 0 or the real-time communication success rate is less than 0, the real-time heartbeat packet sending frequency is calculated as follows: Among them, f rt is the frequency of sending real-time heartbeat packets, f1 is the minimum frequency of the constraint condition, and f2 is the maximum frequency of the constraint condition.
5. The method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy according to claim 1, characterized in that: The step of determining the connection status and performing a retransmission or repair operation according to the real-time monitoring data, and recording a total number of repair operations and a total number of retransmission operations, includes: When no reply is received after the heartbeat packet is sent, a retransmission operation is performed; wherein, the number of retransmission operations after each heartbeat packet is sent cannot exceed the preset retransmission number threshold; Record the retransmission operation, and take the sum of all retransmission times in this cycle as the total number of retransmission operations; When it is determined that an error event occurs or the connection is disconnected, a repair operation is performed; Record the repair operations, and take the sum of all repair times in this cycle as the total number of repair operations.
6. The method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy according to claim 5, characterized in that: When it is determined that an error event occurs or the connection is disconnected, a repair operation is performed, including: When the number of retransmission operations exceeds a preset retransmission number threshold, it is determined that an error event occurs and a repair operation is performed; When the network delay is higher than a preset threshold or the packet loss rate is greater than a preset threshold, it is determined that a connection is disconnected and a repair operation is performed; The repair operation includes re-establishing a connection, switching to a backup link, or increasing the frequency of sending heartbeat packets.
7. The method for maintaining a TCP long connection based on a dynamic heartbeat packet strategy according to claim 1, characterized in that: The generating connection maintenance feedback data according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations includes: Collect real-time monitoring data, total repair operation times, and total retransmission operation times within the current maintenance cycle as periodic maintenance data; Analyze the periodic maintenance data, and generate connection maintenance feedback data according to the data analysis result, wherein the connection maintenance feedback data includes a frequency adjustment factor and an objective function weight update parameter; The connection maintenance feedback data is stored in a structured manner so as to be used in the next round of connection maintenance.
8. A TCP long connection maintenance device based on dynamic heartbeat packet strategy, characterized in that: include: An initial data acquisition module is used to acquire initial network status data, constraint conditions and real-time network status data, wherein the initial network status data includes an initial heartbeat packet sending frequency, multiple historical network fluctuation coefficients and a communication success rate; An objective function calculation module, used to calculate a frequency objective function value according to the initial network state data; A real-time frequency calculation module, used for calculating the real-time heartbeat packet sending frequency according to the frequency objective function value, real-time network status data and constraint conditions; A sending frequency adjustment module, which adjusts the sending frequency of the heartbeat packet based on the real-time heartbeat packet sending frequency; A real-time data monitoring module is used to monitor the sending and receiving status of heartbeat packets in real time and obtain real-time monitoring data, which includes the heartbeat packet sending success rate, receiving confirmation rate, packet loss rate and network delay; A connection status repair module, used to determine the connection status and perform retransmission or repair operations according to the real-time monitoring data, and record the total number of repair operations and the total number of retransmission operations; The feedback data generating module is used to generate connection maintenance feedback data according to the real-time monitoring data, the total number of repair operations and the total number of retransmission operations, and use it as input for the next connection maintenance cycle.
9. An electronic device, characterized in that: It includes a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, and when the processor executes the computer program, it implements the TCP long connection maintenance method based on the dynamic heartbeat packet strategy as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the TCP long connection maintenance method based on the dynamic heartbeat packet strategy as described in any one of claims 1 to 7.