• 应用层使用select进行检测连接状态


    可以参考TCP连接保活机制来设计应用层的连接状态监测,同时需要注意到有两个关键点:

    1.需要使用定时器,这可以通过使用 I/O 复用自身的机制来实现,这点可以先看一下《使用select实现定时任务》
    2.需要设计一个 PING-PONG 的协议。

    设计一个PING-PONG协议的的话,首先就需要一个客户端和服务器都认的固定结构体:

    typedef struct {
        u_int32_t type;
        char data[1024];
    } messageObject;
    
    #define MSG_PING          1
    #define MSG_PONG          2
    #define MSG_TYPE1        11
    #define MSG_TYPE2        21
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    这里把前 4 个字节标识了消息类型,为了简单,这里设计了MSG_PING、MSG_PONG、MSG_TYPE 1和MSG_TYPE 2四种消息类型。
    服务器端的代码pingserver.c如下:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    typedef struct {
        u_int32_t type;
        char data[1024];
    } messageObject;
    
    #define MSG_PING          1
    #define MSG_PONG          2
    #define MSG_TYPE1        11
    #define MSG_TYPE2        21
    
    #define    MAXLINE     4096
    static int count;
    
    int main(int argc, char **argv) {
        if (argc != 3) {
            printf("usage: tcpsever  or ");
            exit(1);
        }
    
        int sleepingTime = atoi(argv[2]);
    
        int listenfd;
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
    
        struct sockaddr_in server_addr;
        bzero(&server_addr, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        server_addr.sin_port = htons(atoi(argv[1]));
    
        int rt1 = bind(listenfd, (struct sockaddr *) &server_addr, sizeof(server_addr));
        if (rt1 < 0) {
            printf("bind failed ");
            exit(1);
        }
    
        int rt2 = listen(listenfd, 1024);
        if (rt2 < 0) {
            printf("listen failed ");
            exit(1);
        }
    
        int connfd;
        struct sockaddr_in client_addr;
        socklen_t client_len = sizeof(client_addr);
    
        if ((connfd = accept(listenfd, (struct sockaddr *) &client_addr, &client_len)) < 0) {
            printf("bind failed ");
            exit(1);
        }
    
        messageObject message;
        count = 0;
    
        for (;;) {
            int n = read(connfd, (char *) &message, sizeof(messageObject));
            if (n < 0) {
                printf("error read");
                exit(1);
            } else if (n == 0) {
                printf("client closed \n");
                continue;
            }
    
            printf("received %d bytes\n", n);
            count++;
    
            switch (ntohl(message.type)) {
                case MSG_TYPE1 :
                    printf("process  MSG_TYPE1 \n");
                    break;
    
                case MSG_TYPE2 :
                    printf("process  MSG_TYPE2 \n");
                    break;
    
                case MSG_PING: {
                    messageObject pong_message;
                    pong_message.type = MSG_PONG;
                    sleep(sleepingTime);
                    ssize_t rc = send(connfd, (char *) &pong_message, sizeof(pong_message), 0);
                    if (rc < 0)
                        printf("send failure");
                    break;
                }
    
                default :
                    printf("unknown message type (%d)\n", ntohl(message.type));
            }
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    gcc pingserver.c -o pingserver进行编译。
    在这里插入图片描述

    客户端的代码pingclient.c如下:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    typedef struct {
        u_int32_t type;
        char data[1024];
    } messageObject;
    
    #define MSG_PING          1
    #define MSG_PONG          2
    #define MSG_TYPE1        11
    #define MSG_TYPE2        21
    
    #define    MAXLINE     4096
    #define    KEEP_ALIVE_TIME  10
    #define    KEEP_ALIVE_INTERVAL  3
    #define    KEEP_ALIVE_PROBETIMES  3
    
    
    int main(int argc, char **argv) {
         if (argc != 3) {
            printf("usage: select01  or ");
        }
    
        int socket_fd;
        socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    
        struct sockaddr_in server_addr;
        bzero(&server_addr, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(atoi(argv[2]));
        inet_pton(AF_INET, argv[1], &server_addr.sin_addr);
    
        socklen_t server_len = sizeof(server_addr);
        int connect_rt = connect(socket_fd, (struct sockaddr *) &server_addr, server_len);
        if (connect_rt < 0) {
            printf("connect failed ");
            exit(1);
        }
    
        char recv_line[MAXLINE + 1];
        int n;
    
        fd_set readmask;
        fd_set allreads;
    
        struct timeval tv;
        int heartbeats = 0;
    
        tv.tv_sec = KEEP_ALIVE_TIME;
        tv.tv_usec = 0;
    
        messageObject messageObject;
    
        FD_ZERO(&allreads);
        FD_SET(socket_fd, &allreads);
        for (;;) {
            readmask = allreads;
            int rc = select(socket_fd + 1, &readmask, NULL, NULL, &tv);
            if (rc < 0) {
                printf("select failed");
                exit(1);
            }
            if (rc == 0) {
                if (++heartbeats > KEEP_ALIVE_PROBETIMES) {
                    printf("connection dead\n");
                    exit(1);
                }
                printf("sending heartbeat #%d\n", heartbeats);
                messageObject.type = htonl(MSG_PING);
                rc = send(socket_fd, (char *) &messageObject, sizeof(messageObject), 0);
                if (rc < 0) {
                    printf("send failure");
                    exit(1);
                }
                tv.tv_sec = KEEP_ALIVE_INTERVAL;
                continue;
            }
            if (FD_ISSET(socket_fd, &readmask)) {
                n = read(socket_fd, recv_line, MAXLINE);
                if (n < 0) {
                    printf("read error");
                    exit(1);
                } else if (n == 0) {
                    printf("server terminated \n");
                    exit(1);
                }
                printf("received heartbeat, make heartbeats to 0 \n");
                heartbeats = 0;
                tv.tv_sec = KEEP_ALIVE_TIME;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    gcc pingclient.c -o pingclient进行编译。
    在这里插入图片描述

    第一种情况:服务器状态不正常

    模拟服务器端崩溃的的现象:./pingserver 8080 60监听在8080端口,然后设置60秒的监听间隔,也就是服务器端接收到PING报文之后,在六十秒内不做任何回应,相当于模拟服务器端出现崩溃的现象。
    客户端执行的是./pingclient 127.0.0.1 8080
    在这里插入图片描述
    在服务器上按下 ctrl + c可以结束程序。
    在这里插入图片描述

    第二种情况:服务器状态正常

    ./pingserver 8080 5监听在8080端口,然后设置5秒的监听间隔,也就是服务器端接收到PING报文之后,在五秒内不做任何回应,这里只是模拟服务器正常处理业务请求。
    客户端执行的是./pingclient 127.0.0.1 8080
    发现只要服务器端不停止,那么客户端就没有停止监测。
    在这里插入图片描述
    可以看到服务器一停止,那么客户端就停止了。
    在这里插入图片描述

    此文章为11月Day 16学习笔记,内容来源于极客时间《网络编程实战》

  • 相关阅读:
    [Ajax]初始Ajax
    多模态领域的先进模型
    操作系统复习第四章:存储器管理
    JavaScript 30. JSON
    18.EC实战 新建项目工程并配置各个引脚的工作方式(持续更新)
    服务器(Linux)查看Tomcat运行日志
    python学习笔记(08)---(内置容器-集合、字符串)
    [JS] 网络请求相关
    SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
    网工内推 | 金融业,网络管理岗,CCIE优先,最高30k
  • 原文地址:https://blog.csdn.net/qq_42108074/article/details/134450684