套接字选项SO_KEEPALIVE
生活随笔
收集整理的這篇文章主要介紹了
套接字选项SO_KEEPALIVE
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ? ? ? ? ? 套接字選項(xiàng)SO_KEEPALIVE是?;钐捉幼?#xff0c;它會(huì)在規(guī)定的時(shí)間內(nèi)給對(duì)端發(fā)送探測(cè)分節(jié),用tcpdump抓包可以看到。這個(gè)規(guī)定的時(shí)間就是net.ipv4.tcp_keepalive_time,在系統(tǒng)中可以動(dòng)態(tài)的設(shè)置它,它的默認(rèn)值是7200s。SO_KEEPLIVE選項(xiàng)由setsockopt函數(shù)進(jìn)行設(shè)置。
服務(wù)端:
?
#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <errno.h> #include <malloc.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <stdarg.h> #include <fcntl.h> #include <time.h>int main() {int sockSrv = socket(AF_INET, SOCK_STREAM, 0);struct sockaddr_in addrSrv;addrSrv.sin_family = AF_INET;addrSrv.sin_addr.s_addr =htonl(INADDR_ANY);addrSrv.sin_port = htons(8888);bind(sockSrv, (const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));listen(sockSrv, 5);sockaddr_in addrClient;socklen_t len=sizeof(addrClient);int sockConn = accept(sockSrv, (struct sockaddr *)&addrClient, &len);int keepAlive=1;setsockopt(sockConn,SOL_SOCKET,SO_KEEPALIVE,(void *)&keepAlive,sizeof(keepAlive));getchar();close(sockConn);close(sockSrv);return 0; }客戶(hù)端:
?
?
#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <errno.h> #include <malloc.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <stdarg.h> #include <fcntl.h> #include <error.h>int main() {int ret=0;int sockClient = socket(AF_INET, SOCK_STREAM, 0);struct sockaddr_in addrSrv;addrSrv.sin_addr.s_addr=inet_addr("127.0.0.1");addrSrv.sin_family = AF_INET;addrSrv.sin_port = htons(8888);ret=connect(sockClient, ( const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));getchar();close(sockClient);return 0; }?
tcpdump抓包結(jié)果:
?
[root@localhost mapan]# tcpdump -iany port 8888 -nlps0 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes 13:57:50.484437 IP 127.0.0.1.40596 > 127.0.0.1.ddi-tcp-1: Flags [S], seq 2495400572, win 65495, options [mss 65495,sackOK,TS val 3129736193 ecr 0,nop,wscale 7], length 0 13:57:50.484453 IP 127.0.0.1.ddi-tcp-1 > 127.0.0.1.40596: Flags [S.], seq 1098484178, ack 2495400573, win 65483, options [mss 65495,sackOK,TS val 3129736193 ecr 3129736193,nop,wscale 7], length 0 13:57:50.484471 IP 127.0.0.1.40596 > 127.0.0.1.ddi-tcp-1: Flags [.], ack 1, win 512, options [nop,nop,TS val 3129736193 ecr 3129736193], length 0 13:59:50.483791 IP 127.0.0.1.ddi-tcp-1 > 127.0.0.1.40596: Flags [.], ack 1, win 512, options [nop,nop,TS val 3129856193 ecr 3129736193], length 0 13:59:50.483802 IP 127.0.0.1.40596 > 127.0.0.1.ddi-tcp-1: Flags [.], ack 1, win 512, options [nop,nop,TS val 3129856193 ecr 3129736193], length 0 14:01:50.483787 IP 127.0.0.1.ddi-tcp-1 > 127.0.0.1.40596: Flags [.], ack 1, win 512, options [nop,nop,TS val 3129976193 ecr 3129856193], length 0 14:01:50.483798 IP 127.0.0.1.40596 > 127.0.0.1.ddi-tcp-1: Flags [.], ack 1, win 512, options [nop,nop,TS val 3129976193 ecr 3129736193], length 0?
服務(wù)端每2分鐘會(huì)發(fā)送一個(gè)探測(cè)分節(jié)給客戶(hù)端,客戶(hù)端以相應(yīng)的ACK回應(yīng),標(biāo)明客戶(hù)端一切正常。
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的套接字选项SO_KEEPALIVE的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: accept函数与TCP三次握手
- 下一篇: 套接字选项SO_LINGER