TCP exception disconnection detection with socket options

Source: Internet
Author: User

A tcp disconnection exception occurs when a sudden power failure or network cable disconnection occurs. If no data transmission or communication is processed by both parties, the connection is disconnected.

In general, in order to prevent socket communication from being restricted by the operating system, you need to implement the heartbeat packet mechanism at the application layer to check for abnormal disconnection, generally, when the server does not receive the client data packet for a period of time, it regularly sends the packet and then the client responds. If an exception occurs, the server returns an error when receiving the packet, if the client does not receive the packet within the specified time, it will send the packet to the server. If an error is returned, it indicates that the packet is disconnected. such methods are self-implemented heartbeat packet mechanism.

However, the operating system itself also comes with some heartbeat packet mechanisms, which are implemented at the underlying layer of the socket TCP stack and do not affect application-layer communication and do not need to be processed by the application layer, when an exception is found to be disconnected, you can check it out and return an error (it is essentially a heartbeat packet when idle ). the following describes the methods in Windows and Linux.

First, we will introduce the method in windows. This method requires both parties to communicate with each other to be Windows NT or later (if it is another version of the operating system, such as Linux, it cannot be guaranteed that 100% is invalid ). in msdn, The sio_keepalive_vals option in wsaioctl is described. This option and the definition of struct tcp_keepalive are in mstcpip. h: see the Code directly without instructions:

 

# Include <mstcpip. h>

 

DWORD retbytes;
Tcp_keepalive inkeepsetting;
Tcp_keepalive retkeepsetting;

Inkeepsetting. Onoff = 1; // Number of probes
Inkeepsetting. KeepAliveTime = 5500; // No idle time for sending and receiving TCP data before the First Probe
Inkeepsetting. keepaliveinterval = 3000; // interval of each test

If (wsaioctl (aptsock, sio_keepalive_vals,
& Inkeepsetting,
Sizeof (inkeepsetting ),
& Retkeepsetting,
Sizeof (retkeepsetting ),
& Retbytes,
Null,
Null )! = 0)
{
Printf ("wsaioctl error: % d/N", wsagetlasterror ());
}

 

In Linux, you can use setsockopt to set options. For details, see the code (the code is excerpted from the network ):

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

 

# Define socket_error (-1)

 

// In seconds
Int keepalive = 1; // set keepalive
Int keepidle = 5; // No idle time for sending and receiving data in TCP before the first test starts
Int keepinterval = 3; // interval of each test
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 );

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.