套接字选项SO_KEEPALIVE

              套接字选项SO_KEEPALIVE是保活套接字,它会在规定的时间内给对端发送探测分节,用tcpdump抓包可以看到。这个规定的时间就是net.ipv4.tcp_keepalive_time,在系统中可以动态的设置它,它的默认值是7200s。SO_KEEPLIVE选项由setsockopt函数进行设置。

服务端:

 

#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;
}

客户端:

 

 

#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抓包结果:

 

[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

 

服务端每2分钟会发送一个探测分节给客户端,客户端以相应的ACK回应,标明客户端一切正常。

 

 

 

 

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼盼编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值