Non-normal Disconnect detection method for the other network

Source: Internet
Author: User
Under Windows, "abnormal disconnect" refers to the TCP connection is not graceful way to disconnect, such as network cable failure, such as the reasons for physical links, as well as sudden host power outages, and other reasons there are two ways to detect: 1.TCP Connect Both sides timed handshake message 2. Using keepalive detection in TCP protocol stack The second method is simple and reliable, only the TCP connection two socket set keepalive detection, so this article only said the second method in the implementation of the linux,window2000 (no further testing on other platforms) Windows 2000 platform Bottom file # Include <mstcpip.h>//definition structure and macro struct tcp_keepalive {u_longonoff; u_longkeepalivetime; u_longkeepaliveinterval;};     1. Tcp_keepalive live,liveout;   &NBSP;&NBSP;2. live.keepaliveinterval=500;     3. live.keepalivetime=3000;     4. Live.onoff=true;     5. int iRet = setsockopt (socket,sol_socket,so_keepalive, (char *) opt,sizeof (int));     6. if (IRet = = 0) {    7.      8.          DWORD DW;     9.     10.         if (WSAIoctl, Socket,sio_keepalive_vals,&live,sizeof (live), &liveout,sizeof (liveout), &dw,null,null) = = Socket_error) {&nbsp  11.     12.                 //delete Client   &NBSP;13.     14.                 return;   &NBSP;15. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}    16. &NBSP;&NBSP;&NBSP}       ace code//by rainfish    blog.csdn.net/bat603       int Opt = 1;   //in the testing process, found that the number of detection is 5 times, that is, the following settings, from the most recent message start calculated 10 seconds after each interval of 5 seconds, continuously sent 5 times, that is   //35 seconds to find the network broke tcp_keepalive Live,liveout;   live.keepaliveinterval=5000; Interval per detection (in milliseconds) live.keepalivetime=10000;  //the first time to start sending (in milliseconds) live.onoff=true;   int iRet = stream.set_option (sol_socket,so_keepalive,&opt,sizeof (int));   if (IRet = 0) {                     dword DW;               //This shows the method of acquiring sockets under Aces, that is, the handle (socket) is a handle            if (WSAIoctl (SOCKET) h,sio_keepalive_vals,&live, sizeof (live), &liveout,sizeof (liveout), &dw,null,null) = = Socket_error) {                         // Delete Client                            return;   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}        }         Linux platform #include     "/usr/include/linux/tcp.h" #include "/usr/include/linux/ Socket.h "////keepalive Implementation, Unit SEC/The following code requires ACE, if no ACE is included, please change the ACE function used to linux corresponding interface int keepAlive = 1;//set keepAlive int Keepidle = 5;//start of the first keepalive probe before the TCP null-closed time int Keepinterval = 5;//time interval int keepcount = Two KEEPALIVE probes 3;//determine the number of KEEPALIVE probes before disconnecting   if (setsockopt ( void*) &keepalive,sizeof (keepAlive)) = = 1) {Ace_debug (Lm_info, Ace_text ("(%p|%t) setsockopt so_keepalive error!/ n "));  }   if (SetSockOpt (S,sol_tcp,tcp_keepidle, (void *) &keepidle,sizeof (keepidle)) = = 1) {Ace_debug (lm_ INFO, Ace_text ("(%p|%t) setsockopt tcp_keepidle error!/n")); }   if (SetSockOpt (S,SOL_TCP,TCP_KEEPINTVL, (void *) &keepinterval,sizeof (keepinterval)) = = 1) {Ace_debug (LM _info, Ace_text ("(%p|%t) setsockopt TCP_KEEPINTVL error!/n")); }   if (SetSockOpt (s,sol_tcp,tcp_keepcnt, (void *) &keepcount,sizeof (keepcount)) = = 1) {Ace_debug (Lm_info, Ace_text ("(%p|%t) setsockopt tcp_keepcnt error!/n")); }    

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.