DSP

NDK-TCP连接异常断开检测

2019-07-13 16:52发布

TCP是一种面向连接的协议,连接的建立和断开需要通过收发相应的分节来实现。某些时候,由于网络的故障或是一方主机的突然崩溃而另一方无法检测到,以致始终保持着不存在的连接。下面介绍一种方法来检测这种异常断开的情况 1) 在TCP协议中提供了KEEPALIVE检测。该选项使能后,在一个TCP连接上,若指定的一段时间内没有数据交换,则自动发送分节等待对方确认。      SO_KEEPALIVE : 该选项设置是否打开探测
             TCP_KEEPIDLE : 开始发送探测分节前等待的空闲时间
             TCP_KEEPINTVL: 两次发送探测分节的时间间隔
             TCP_KEEPCNT: 判定断开前发送探测分节的次数 2) 设定探测相关选项值      int keepalive = 1;             // 打开探测
             int keepidle = 60;        // 开始探测前的空闲等待时间
             int keepintvl = 10;        // 发送探测分节的时间间隔
             int keepcnt = 3;        // 发送探测分节的次数 3) 设置套接字的属性      if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive, sizeof (keepalive) < 0)
             {
                     perror(“fail to set SO_KEEPALIVE”);
                     exit(-1);
             }
             if (setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, (void *) &keepidle, sizeof (keepidle) < 0)
             {
                     perror(“fail to set SO_KEEPIDLE”);
                     exit(-1);
             }
             if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (void *)&keepintvl, sizeof (keepintvl) < 0)
             {
                     perror(“fail to set SO_KEEPINTVL”);
                     exit(-1);
             }
             if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (void *)&keepcnt, sizeof (keepcnt) < 0)
             {