CN103716323A - Method for maintaining long connection by sending heartbeat packet - Google Patents

Method for maintaining long connection by sending heartbeat packet Download PDF

Info

Publication number
CN103716323A
CN103716323A CN201310747400.4A CN201310747400A CN103716323A CN 103716323 A CN103716323 A CN 103716323A CN 201310747400 A CN201310747400 A CN 201310747400A CN 103716323 A CN103716323 A CN 103716323A
Authority
CN
China
Prior art keywords
client
keepalive
service end
tcp
socket
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN201310747400.4A
Other languages
Chinese (zh)
Other versions
CN103716323B (en
Inventor
严伯盛
林飞达
黄礼锋
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Xiamen Yuexin Information Technology Co ltd
Original Assignee
XIAMEN YUEXUN INFORMATION TECHNOLOGY Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by XIAMEN YUEXUN INFORMATION TECHNOLOGY Co Ltd filed Critical XIAMEN YUEXUN INFORMATION TECHNOLOGY Co Ltd
Priority to CN201310747400.4A priority Critical patent/CN103716323B/en
Priority claimed from CN201310747400.4A external-priority patent/CN103716323B/en
Publication of CN103716323A publication Critical patent/CN103716323A/en
Application granted granted Critical
Publication of CN103716323B publication Critical patent/CN103716323B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Landscapes

  • Communication Control (AREA)
  • Computer And Data Communications (AREA)

Abstract

A method for maintaining the long connection by sending a heartbeat packet includes the steps that a keepAlive switch is turned on, keepAlive parameters are set, a keepAlive timer of a client TCP starts to work, and if no effective data transmission happens before time of the keepAlive timer is up, the client TCP stack working in a kernel space of the system automatically sends the keep-alivepacket to a server side. According to different network conditions, three processing procedures of normal conditions, network abnormity or server side abnormity and the condition that the server side is restarted after being interrupted occur. The method is achieved by regulating the keepAlive parameters of the TCP, the function that the keepAlive options of the TCP are used is the internal function of the TCP and belongs to a transmission layer, when a server receives the keepAlive explorer packet sent by the client side, the server can reply the client side on the transmission layer, the keepAlive explorer packet will not be submitted to the upper layer any more, and therefore the efficiency is improved greatly.

Description

A kind of heartbeat packet that sends maintains the long method connecting
Technical field
The present invention relates to the communications field, be specifically related to a kind of heartbeat packet that sends and maintain the long method connecting.
Background technology
Existing heartbeat packet technology is all to send data to server by application layer, and server is replied client after receiving heartbeat packet data, and client receives that the reply confirmation of server connects normally, does not receive that normal reply of server is judged as the abnormal interruption of connection.The major defect of this technology is a lot of server resources of waste, because data need to copy to operating system nucleus space from network interface card spatial cache, to copy to user's space from operating system nucleus space again, the data that server is replied also need to copy to operating system nucleus space from user's space, then copy to network interface card spatial cache from kernel spacing.As shown in Figure 1.If separate unit server exists, a large amount of (hundreds of thousands is up to a million) are long to be connected, heartbeat packet interval time very short (being less than 2 minutes) that each length connects simultaneously, and at this moment server can switch very frequently between kernel and user's space, and performance waste is serious.
In view of this, the inventor, for the defect further investigation of prior art, has this case to produce then.
Summary of the invention
Technical problem to be solved by this invention is to provide a kind of high efficiency transmission heartbeat packet to maintain the long method connecting, by adjusting the keepAlive parameter of TCP, realize, with the keepAlive option of Transmission Control Protocol, it is the function of Transmission Control Protocol itself, belong to transport layer, when receiving the keepAlive detection packet that client sends, server just can reply client in transport layer, can't to upper strata, submit to again, so efficiency is high a lot.
The present invention is achieved in that
Send heartbeat packet and maintain the long method connecting, comprise the steps:
Step 1: open keepAlive switch, keepAlive parameter is set, specifically comprise:
Client:
Steps A 1: set up TCP Socket client;
Steps A 2: open SO_KEEPALIVE option;
Steps A 3: it is heartbeat packet gap periods that TCP_KEEPIDLE is set;
Steps A 4: it is s second that TCP_KEEPINTVL is set;
Steps A 5: it is n time that TCP_KEEPCNT is set;
Steps A 6: start block type read-write;
Service end:
Step B1: set up TCP Socket service end;
Step B2: select asynchronous unblock read-write;
Step B3: process regular traffic logic, do not process heartbeat packet code;
The keepalive timer of step 2: client TCP is started working; if there is not valid data transmission before keepalive timer expires; the client tcp protocol stack that is operated in the kernel spacing of system is initiated a keep-alive packet from trend service end; this keep-alive packet is exactly that ACK and current TCP sequence number subtract one combination; now according to the different situations of network, there will be that normal condition, Network Abnormal or service end are abnormal, restart three kinds of handling processes after service end once-interrupted:
If normal condition proceeds to step C1:
Step C1: client is initiated keepAlive detection packet to service end;
Step C2: service end is received keepAlive detection packet;
Step C3: service end is sent ACK and replied bag;
Step C4: client receives that ACK replys bag;
Step C5: client keepalive timer restarts timing, enters circulation next time;
If Network Abnormal, or service end is abnormal, proceeds to step D1:
Step D1: client is initiated keepAlive detection packet to service end;
Step D2: after the time of setting through TCP_KEEPINTVL, client is not received reply, again sends keepAlive detection packet, repeats the number of times that TCP_KEEPCNT sets;
Step D3: client does not receive that ACK replys bag, causes SOCKET_ERROR always;
Step D4: kernel spacing is submitted SOCKET_ERROR to user's space;
Step D5: user space application detects SOCKET_ERROR, recv function returns to SOCKET_ERROR, the result that WSAGetLastError () obtains is WSAECONNRESET (10054), re-establishes new SOCKET and is connected with service end;
If restart after service end once-interrupted, proceed to step e 1:
Step e 1: client is initiated keepAlive detection packet to service end;
Step e 2: service end is received keepAlive detection packet;
Step e 3: service end is sent the reset of RST sign and replied bag;
Step e 4: client is received RST sign reset reply bag;
Step e 5: client is initiated SocketException: distance host forced closing an existing connection paraphasia, close current socket;
Step e 6: client kernel spacing is submitted SocketException to the user program of user's space;
Step e 7: user space application detects SocketException, re-establishes new SOCKET and is connected with service end.
Further, the s in described steps A 4 is 5; N in described steps A 5 is 2.
The invention has the advantages that: by service end Transmission Control Protocol self character, directly in the complete heartbeat packet of transport layer process, be not submitted to application layer, greatly reduced the time of processing heartbeat packet, improved the performance of server.
Accompanying drawing explanation
The invention will be further described in conjunction with the embodiments with reference to the accompanying drawings.
Fig. 1 is the method schematic diagram of present technology.
Fig. 2 is method schematic diagram of the present invention.
Fig. 3 is the schematic flow sheet of network normal condition in the present invention.
Fig. 4 is Network Abnormal in the present invention, or the abnormal schematic flow sheet of service end.
Fig. 5 is the schematic flow sheet of restarting after service end once-interrupted in the present invention.
Embodiment
As shown in Figure 2, the keepAlive option with Transmission Control Protocol that the present invention proposes is the function of Transmission Control Protocol own, belongs to transport layer, when server is received the keepAlive detection packet that client sends, in transport layer, just can reply client, can't to upper strata, submit to again, so efficiency is high a lot.
A kind of heartbeat packet that sends of the present invention maintains the long method connecting, and comprises the steps:
Step 1: open keepAlive switch, keepAlive parameter is set, specifically comprise:
Client:
Steps A 1: set up TCP Socket client;
Steps A 2: open SO_KEEPALIVE option;
Steps A 3: it is heartbeat packet gap periods that TCP_KEEPIDLE is set;
Steps A 4: it is 5 seconds that TCP_KEEPINTVL is set;
Steps A 5: it is 2 times that TCP_KEEPCNT is set;
Steps A 6: start block type read-write;
Service end:
Step B1: set up TCP Socket service end;
Step B2: select asynchronous unblock read-write;
Step B3: process regular traffic logic, do not process heartbeat packet code;
The keepalive timer of step 2: client TCP is started working; if there is not valid data transmission before keepalive timer expires; the client tcp protocol stack that is operated in the kernel spacing of system is initiated a keep-alive packet from trend service end; this keep-alive packet is exactly that ACK and current TCP sequence number subtract one combination; now according to the different situations of network, there will be that normal condition, Network Abnormal or service end are abnormal, restart three kinds of handling processes after service end once-interrupted:
If normal condition as shown in Figure 3, proceeds to step C1:
Step C1: client is initiated keepAlive detection packet to service end;
Step C2: service end is received keepAlive detection packet;
Step C3: service end is sent ACK and replied bag;
Step C4: client receives that ACK replys bag;
Step C5: client keepalive timer restarts timing, enters circulation next time.
From above flow process, can find out, the all-router of keepAlive detection packet process and NAT and other network equipment are all equivalent to the effect of heartbeat packet under normal circumstances, refreshed the route record table of these network equipments, whole communication process all completes in the kernel spacing of system simultaneously, and the treatment effeciency of service end improves greatly.
If Network Abnormal, or service end is abnormal, as shown in Figure 4, proceeds to step D1:
Step D1: client is initiated keepAlive detection packet to service end;
Step D2: after the time of setting through TCP_KEEPINTVL, client is not received reply, again sends keepAlive detection packet, repeats the number of times that TCP_KEEPCNT sets;
Step D3: client does not receive that ACK replys bag, causes SOCKET_ERROR always;
Step D4: kernel spacing is submitted SOCKET_ERROR to user's space;
Step D5: user space application detects SOCKET_ERROR, recv function returns to SOCKET_ERROR, the result that WSAGetLastError () obtains is WSAECONNRESET (10054), re-establishes new SOCKET and is connected with service end.
From above flow process, can find out, under abnormal conditions, keepAlive detection packet plays abnormal interruption discovery feature, the application program of the user's space of service end just needs corresponding processing when only client reconnects after Network Abnormal occurs, the application program of other situation service end is transparent, has reduced the resource consumption that a large amount of service ends are processed heartbeat packet.
If restart after service end once-interrupted, as shown in Figure 5, proceed to step e 1:
Step e 1: client is initiated keepAlive detection packet to service end;
Step e 2: service end is received keepAlive detection packet;
Step e 3: service end is sent the reset of RST sign and replied bag;
Step e 4: client is received RST sign reset reply bag;
Step e 5: client is initiated SocketException: distance host forced closing an existing connection paraphasia, close current socket;
Step e 6: client kernel spacing is submitted SocketException to the user program of user's space;
Step e 7: user space application detects SocketException, re-establishes new SOCKET and is connected with service end.
The present invention directly in the complete heartbeat packet of transport layer process, is not submitted to application layer by service end Transmission Control Protocol self character, has greatly reduced the time of processing heartbeat packet, has improved the performance of server.
The foregoing is only better enforcement use-case of the present invention, be not intended to limit protection scope of the present invention.Within the spirit and principles in the present invention all, any modification of doing, be equal to and replace and improvement etc., within all should being included in protection scope of the present invention.

Claims (2)

1. send heartbeat packet and maintain the long method connecting, it is characterized in that: comprise the steps:
Step 1: open keepAlive switch, keepAlive parameter is set, specifically comprise:
Client:
Steps A 1: set up TCP Socket client;
Steps A 2: open SO_KEEPALIVE option;
Steps A 3: it is heartbeat packet gap periods that TCP_KEEPIDLE is set;
Steps A 4: it is s second that TCP_KEEPINTVL is set;
Steps A 5: it is n time that TCP_KEEPCNT is set;
Steps A 6: start block type read-write;
Service end:
Step B1: set up TCP Socket service end;
Step B2: select asynchronous unblock read-write;
Step B3: process regular traffic logic, do not process heartbeat packet code;
The keepalive timer of step 2: client TCP is started working; if there is not valid data transmission before keepalive timer expires; the client tcp protocol stack that is operated in the kernel spacing of system is initiated a keep-alive packet from trend service end; this keep-alive packet is exactly that ACK and current TCP sequence number subtract one combination; now according to the different situations of network, there will be that normal condition, Network Abnormal or service end are abnormal, restart three kinds of handling processes after service end once-interrupted:
If normal condition proceeds to step C1:
Step C1: client is initiated keepAlive detection packet to service end;
Step C2: service end is received keepAlive detection packet;
Step C3: service end is sent ACK and replied bag;
Step C4: client receives that ACK replys bag;
Step C5: client keepalive timer restarts timing, enters circulation next time;
If Network Abnormal, or service end is abnormal, proceeds to step D1:
Step D1: client is initiated keepAlive detection packet to service end;
Step D2: after the time of setting through TCP_KEEPINTVL, client is not received reply, again sends keepAlive detection packet, repeats the number of times that TCP_KEEPCNT sets;
Step D3: client does not receive that ACK replys bag, causes SOCKET_ERROR always;
Step D4: kernel spacing is submitted SOCKET_ERROR to user's space;
Step D5: user space application detects SOCKET_ERROR, recv function returns to SOCKET_ERROR, the result that WSAGetLastError () obtains is WSAECONNRESET (10054), re-establishes new SOCKET and is connected with service end;
If restart after service end once-interrupted, proceed to step e 1:
Step e 1: client is initiated keepAlive detection packet to service end;
Step e 2: service end is received keepAlive detection packet;
Step e 3: service end is sent the reset of RST sign and replied bag;
Step e 4: client is received RST sign reset reply bag;
Step e 5: client is initiated SocketException: distance host forced closing an existing connection paraphasia, close current socket;
Step e 6: client kernel spacing is submitted SocketException to the user program of user's space;
Step e 7: user space application detects SocketException, re-establishes new SOCKET and is connected with service end.
2. a kind of heartbeat packet that sends as claimed in claim 1 maintains the long method connecting, and it is characterized in that: the s in described steps A 4 is 5; N in described steps A 5 is 2.
CN201310747400.4A 2013-12-31 A kind of heartbeat packet that sends maintains the long method connected Active CN103716323B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201310747400.4A CN103716323B (en) 2013-12-31 A kind of heartbeat packet that sends maintains the long method connected

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201310747400.4A CN103716323B (en) 2013-12-31 A kind of heartbeat packet that sends maintains the long method connected

Publications (2)

Publication Number Publication Date
CN103716323A true CN103716323A (en) 2014-04-09
CN103716323B CN103716323B (en) 2016-11-30

Family

ID=

Cited By (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105049438A (en) * 2015-08-05 2015-11-11 浪潮电子信息产业股份有限公司 Method and device for resetting socket connection
WO2017005118A1 (en) * 2015-07-09 2017-01-12 阿里巴巴集团控股有限公司 Method, device, terminal and server for maintaining communication connection
CN107124324A (en) * 2016-02-25 2017-09-01 阿里巴巴集团控股有限公司 A kind of heart-beat protocol method and apparatus based on lease
CN107276859A (en) * 2017-08-21 2017-10-20 武汉斗鱼网络科技有限公司 Keep the method and device of long connection
CN107819648A (en) * 2017-11-14 2018-03-20 新华三技术有限公司 Network configuration NETCONF connecting detection method and apparatus
CN109067698A (en) * 2018-06-05 2018-12-21 中国平安人寿保险股份有限公司 A kind of variation and equipment of document of agreement
CN110225084A (en) * 2019-05-06 2019-09-10 北京口袋时尚科技有限公司 A kind of method and device keeping TCP long connection
CN111356017A (en) * 2018-12-24 2020-06-30 浙江宇视科技有限公司 Video monitoring network equipment keep-alive method and device
CN114793202A (en) * 2022-04-25 2022-07-26 珠海格力电器股份有限公司 Communication method, device, equipment and system

Cited By (17)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2017005118A1 (en) * 2015-07-09 2017-01-12 阿里巴巴集团控股有限公司 Method, device, terminal and server for maintaining communication connection
CN105049438B (en) * 2015-08-05 2018-02-23 浪潮电子信息产业股份有限公司 A kind of method and apparatus of reset socket connections
CN105049438A (en) * 2015-08-05 2015-11-11 浪潮电子信息产业股份有限公司 Method and device for resetting socket connection
CN107124324B (en) * 2016-02-25 2020-09-01 阿里巴巴集团控股有限公司 Heartbeat protocol method and equipment based on lease
US10601930B2 (en) 2016-02-25 2020-03-24 Alibaba Group Holding Limited Lease-based heartbeat protocol method and apparatus
CN107124324A (en) * 2016-02-25 2017-09-01 阿里巴巴集团控股有限公司 A kind of heart-beat protocol method and apparatus based on lease
CN107276859A (en) * 2017-08-21 2017-10-20 武汉斗鱼网络科技有限公司 Keep the method and device of long connection
CN107276859B (en) * 2017-08-21 2019-10-25 武汉斗鱼网络科技有限公司 Keep the method and device of long connection
CN107819648B (en) * 2017-11-14 2020-08-04 新华三技术有限公司 Method and device for detecting NETCONF connection of network configuration
CN107819648A (en) * 2017-11-14 2018-03-20 新华三技术有限公司 Network configuration NETCONF connecting detection method and apparatus
CN109067698A (en) * 2018-06-05 2018-12-21 中国平安人寿保险股份有限公司 A kind of variation and equipment of document of agreement
CN111356017B (en) * 2018-12-24 2022-05-13 浙江宇视科技有限公司 Video monitoring network equipment keep-alive method and device
CN111356017A (en) * 2018-12-24 2020-06-30 浙江宇视科技有限公司 Video monitoring network equipment keep-alive method and device
CN110225084A (en) * 2019-05-06 2019-09-10 北京口袋时尚科技有限公司 A kind of method and device keeping TCP long connection
CN110225084B (en) * 2019-05-06 2022-06-14 北京口袋时尚科技有限公司 Method and device for maintaining long connection of TCP
CN114793202A (en) * 2022-04-25 2022-07-26 珠海格力电器股份有限公司 Communication method, device, equipment and system
CN114793202B (en) * 2022-04-25 2023-11-24 珠海格力电器股份有限公司 Communication method, device, equipment and system

Similar Documents

Publication Publication Date Title
US9774704B2 (en) Home gateway, cloud server, and method for communication therebetween
CN109660639B (en) Data uploading method, device, system and medium
CN107919994B (en) Method and server for realizing hot standby of network service dual-computer
CN105407008A (en) Reconnecting method and system for interrupted TCP (Transmission Control Protocol) connection, terminal and server
US9819640B2 (en) Method for keeping remote connection, electronic device and server
CN103475751A (en) Method and device for IP address switch
WO2019085923A1 (en) Data processing method and device, and computer
CN104468380A (en) Interface communication method of access point device and server interaction
US20110225230A1 (en) Method and apparatus for detecting active and orphan session-based connections
CN111130886A (en) Network port switching method and device, board card and network port switching system
CN104486187B (en) A kind of CAN communication device and method of dynamic synchronization
CN102413179A (en) Automatic power-on or wake-up machine and power-on or wake-up method thereof
CN103384265B (en) The application layer implementation method of GVRP agreement and system
CN103746842A (en) Method and system for detecting fault restarting of line card
CN109286575B (en) Message priority configuration method, device and network equipment
WO2012132101A1 (en) Information processing device, and failure response program
CN116170337A (en) Method, device and system for testing reliability of network equipment
CN114338477B (en) Communication link monitoring method, device, equipment and storage medium
CN108174398B (en) Data processing method, system and equipment of terminal equipment
CN104660506B (en) A kind of method, apparatus and system of data packet forwarding
CN103716323A (en) Method for maintaining long connection by sending heartbeat packet
EP2564578B1 (en) Port teaming
CN102752365A (en) Method and device for processing information
CN112612670B (en) Session information statistical method, device, exchange equipment and storage medium
CN104333469A (en) Method and system for managing fiber channel (FC) sessions of storage system

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C14 Grant of patent or utility model
GR01 Patent grant
CP03 Change of name, title or address

Address after: 361006 Units 1103 and 1104, Building A3, 339 Chengyi Street, Phase III Software Park, Jimei District, Xiamen City, Fujian Province

Patentee after: Xiamen Yuexin Information Technology Co.,Ltd.

Address before: Room 250, 555 Longhushan Road, Siming District, Xiamen City, Fujian Province, 361000

Patentee before: XIAMEN YUEXUN INFORMATION TECHNOLOGY Co.,Ltd.

CP03 Change of name, title or address
PE01 Entry into force of the registration of the contract for pledge of patent right

Denomination of invention: A method of sending heartbeat packets to maintain long connection

Effective date of registration: 20221213

Granted publication date: 20161130

Pledgee: Bank of China Limited Xiamen hi tech Park sub branch

Pledgor: Xiamen Yuexin Information Technology Co.,Ltd.

Registration number: Y2022980027437

PE01 Entry into force of the registration of the contract for pledge of patent right