TCP exception disconnect detection with socket option

Source: Internet
Author: User

TCP abnormal disconnection refers to a sudden power outage, directly unplug the network cable, and so on, if the two parties do not communicate data transmission and other processing, can not know that the connection has been disconnected.

In the usual case, in order to make the socket communication is not limited by the operating system, it is necessary to implement the heartbeat packet mechanism in the application layer, to check the abnormal disconnection situation, the general way is the server in a period of time did not receive the client packet, the timing of the contract, and then the client response, If an exception has already occurred, the server receives an error, and the client does not receive the packet within the specified time, and then proactively contracts the server, resulting in an error stating the disconnection. The way you do that is the heartbeat packet mechanism that you implement.

But the operating system itself also comes with some heartbeat packet mechanism, these mechanisms are implemented by the socket TCP stack, do not affect the application layer of communication, and do not need to handle the application layer itself, found that abnormal disconnection can self-check out and return the error (its nature is also in idle send Heartbeat packet). Here's a look at the methods under Windows and Linux.

First, the method under Windows is introduced, which requires that both sides of the communication must be Windows NT operating system (if other versions of the operating system, such as Linux, etc., do not guarantee that 100% is invalid). MSDN has a description of the sio_keepalive_vals option in WSAIoctl, this option, and the definition of the struct tcp_keepalive in MSTCPiP.h, without explanation, directly read the code:

#include <MSTCPiP.h>

DWORD retbytes;
Tcp_keepalive inkeepsetting;
Tcp_keepalive retkeepsetting;

Inkeepsetting.onoff = 1; Number of probes
Inkeepsetting.keepalivetime = 5500; TCP no data send and receive idle time before first probe start
Inkeepsetting.keepaliveinterval = 3000; Time between each probe

if (WSAIoctl (Aptsock, Sio_keepalive_vals,
&inkeepsetting,
sizeof (inkeepsetting),
&retkeepsetting,
sizeof (retkeepsetting),
&retbytes,
Null
NULL)! = 0)
{
printf ("WSAIoctl Error:%d/n", WSAGetLastError ());
}

The way to do Linux is to set the options through setsockopt, see Code (snippets from the Web):

#include <netinet/tcp.h>
......

#define SOCKET_ERROR (-1)

In units of seconds
int keepAlive = 1; Set KeepAlive
int keepidle = 5; TCP no data send and receive idle time before first probe start
int keepinterval = 3; Time between each probe
int keepcount = 2; Number of probes

if (setsockopt (s,sol_socket,so_keepalive, (void*) &keepalive,sizeof (KEEPALIVE)) = = Socket_error)

printf ("Call setsockopt error, errno is%d/n", errno);


if (setsockopt (S,sol_tcp,tcp_keepidle, (void *) &keepidle,sizeof (keepidle)) = = Socket_error)

printf ("Call setsockopt error, errno is%d/n", errno);

if (setsockopt (S,SOL_TCP,TCP_KEEPINTVL, (void *) &keepinterval,sizeof (keepinterval)) = = Socket_error)

printf ("Call setsockopt error, errno is%d/n", errno);

if (setsockopt (s,sol_tcp,tcp_keepcnt, (void *) &keepcount,sizeof (keepcount)) = = Socket_error)

printf ("Call setsockopt error, errno is%d/n", errno);

TCP exception disconnect detection with socket option

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.