Network cable broken off detection

Source: Internet
Author: User
Tags time interval keep alive
1. The internal mechanism by which Linux performs write on the socket. It may involve some basic I/O mechanisms, and I can't figure out what's going on, running server and client separately, and then terminating the server so that client continues to write to the already established socket connection, and the first write successfully returns , the second execution process exits without any hint, and there's no return.
Answer: After the server process is aborted, the socket is closed and fin is sent to the client process. If the client process writes data to the closed server socket (the first write), it causes an RST to be sent on the servers side. If the client continues to write data to the socket that receives the RST (second write), then the Sigpipe signal will start.

2.socket disconnecting network cable after detection: http://blog.csdn.net/whb_fei/article/details/54913457
TCP Socket Network cable disconnect judgment

Some network applications in the case of cable disconnection and reconnect, the TCP socket connection remains establish state, if the application does not use TCP KeepAlive, after the network cable disconnects, the previously established socket link will remain in the establish The state does not change. In fact, the TCP protocol to this part is processed, requires a service-side program, when configuring the socket properties, using keepalive option, once there is this configuration, these long-time data-free links will be based on the TCP KeepAlive kernel properties, at greater than (tcp_ Keepalive_time (Tcp_keepalive_probes * TCP_KEEPALIVE_INTVL)), disconnect these links after the corresponding time (in seconds).

About keep alive regardless of windows, or linux,keepalive three parameters:

Sk->keepalive_probes: Number of probes

Sk->keepalive_time: Timeout for probing

SK->KEEPALIVE_INTVL: Detection interval

For an already established TCP connection, if the two parties do not have any packet transmission during the keepalive_time time, the one end of the KeepAlive function will send the eepalive packet, and if no answer is received, every keepalive_ INTVL time to send the packet again, sending keepalive_probes times. Has not received an answer, the Send RST package closes the connection. If an answer is received, the timer is zeroed. For example ★:

Sk->keepalive_probes = 3;

Sk->keepalive_time = 30;

SK->KEEPALIVE_INTVL = 1;

This means that for a TCP connection, the keepalive will not be triggered if there is data traffic on the socket, but if there is no data traffic for 30 seconds, then keep alive begins to work: Sending the probe packet, responding to the thought that the network is good, ending the probe , if there is no corresponding to every 1 seconds to send a probe packet, sent 3 times, 3 times still no corresponding, then shut down the connection, that is, from the network began to disconnect your socket can realize the network anomaly, up to 33 seconds. However, if you do not set keep alive, you may be on the top of your socket (blocking), receive: Recv will always block cannot return, unless the peer actively shut down the connection, because recv do not know that the socket is broken. Send: Depending on the size of the data volume, as long as the underlying protocol station buffer can drop your sending data, the application level of send will continue to successfully return until the buffer is full, even the buffer is full and will block for some time to wait for buffer idle, So your check on the return value of send does not detect a failure at all.

The Keep Alive feature is turned on, and you can tell whether the network is abnormal by sending a return value directly from the Received function. Set the method (Application layer):

int keepalive = 1; Turn on the KeepAlive property

int keepidle = 60; If the connection does not have any data in 60 seconds, the probe

int keepinterval = 5; The time interval for the packet to be detected is 5 seconds

int keepcount = 3; The number of attempts to probe. If the 1th probe packet receives a response, it will not be sent 2 times.

SetSockOpt (RS, Sol_socket, so_keepalive, (void *) &keepalive, sizeof (KEEPALIVE));

SetSockOpt (RS, Sol_tcp, Tcp_keepidle, (void*) &keepidle, sizeof (Keepidle));

SetSockOpt (RS, Sol_tcp, TCP_KEEPINTVL, (void *) &keepinterval, sizeof (Keepinterval));

SetSockOpt (RS, Sol_tcp, tcp_keepcnt, (void *) &keepcount, sizeof (Keepcount));
The corresponding header file is # include <netinet/tcp.h>

Test Results

According to the above example ★ value on the socket on one end to open keep alive, and then block in a recv or non-stop send, this time unplug the network cable, test from unplug the network cable to Recv/send return to the time of failure.

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.