keepalive

1. keepalive:

Transmission Control Protocol (TCP) keepalives are an optional feature, and if included must default to off. The keepalive packet contains null data. In an Ethernet network, a keepalive frame length is 60 bytes, while the server response to this, also a null data frame, is 54 bytes.There are three parameters related to keepalive:

  • Keepalive time is the duration between two keepalive transmissions in idle condition. TCP keepalive period is required to be configurable and by default is set to no less than 2 hours.
  • Keepalive interval is the duration between two successive keepalive retransmissions, if acknowledgement to the previous keepalive transmission is not received.
  • Keepalive retry is the number of retransmissions to be carried out before declaring that remote end is not available.

对于一个已经建立的tcp连接。如果在keepalive_time时间内双方没有任何的数据包传输,则开启keepalive功能的一端将发送 keepalive数据包,若没有收到应答,则每隔keepalive_intvl时间再发送该数据包,发送keepalive_retry次。一直没有收到应答,则发送rst包关闭连接。若收到应答,则将计时器清零。

2.keepalive的使用:

void active_keepalive(int sockfd)
{
        int optval = 1;
        int optlen = sizeof(optval);
        if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
                close(sockfd);
        }   
}

void set_keepalive_params(int sockfd, int idle, int count, int intvl)
{
        int keepalive_time = idle;
        int keepalive_probes = count;
        int keepalive_intvl = intvl;
            
        if (setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, &keepalive_time, sizeof(int)) < 0) {    
                perror("TCP_KEEPIDLE failed");
                return;
        }   
                    
        if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, &keepalive_probes, sizeof(int)) < 0) {
                perror("TCP_KEEPCNT failed");
                return;
        }   
            
        if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, &keepalive_intvl, sizeof(int)) < 0) {
                perror("TCP_KEEPINTVL failed");
                return;
        }   
            
        return;
}


 

3. keepalive测试结果:

前提:在client和server两端正常连接后拔掉网线。

3.1如果keepalive检测到socket断了,那么netstat是显示不出socket的状态的(即为closed),此时再进行write/recv操作就会返回-1,errno=110

3.2如果在keepalive检测到之前执行write,那么write能执行成功,返回写到发送缓冲区的字节数。而这些数据就一直在发送缓冲区上。并且就算keepalive检测到了,netstat还是能显示socket的状态为established。大概要过15-17分钟,netstat才显示为closed。这是因为当TCP发现有数据在发送缓冲区时,会进行重发试图将数据发送到接收端。但如果尝试一定次数后仍无效就会将该socket关闭。

3.3如果在keepalive检测到之前执行recv,如果接收缓冲区上有数据,那么直接返回成功接收到的数据字节数,否则等到keepalive检测到也会马上返回-1,errno=110.

 

ps:个人理解,keepalive最大的作用就是针对阻塞型的recv可以让其快速返回。

 

 

4.遗留问题:

4.1keepalive在发送或接收缓冲区有数据时,不能起作用么?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值