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在发送或接收缓冲区有数据时,不能起作用么?