net/heartbeat/heartbeat_client.c
void sig_handler(int signum) {

    struct tcp_info info;
    int len = sizeof(info);

    if (signum == SIGURG) {
        if (getsockopt(serverfd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t * ) & len) == 0) {
            if ((info.tcpi_state != TCP_ESTABLISHED)) {
                printf("socket is not ready. cannot ping.\n");
                return;
            }
        } else {
            printf("socket error: %s\n", strerror(errno));
            return;
        }

        char c;
        if (recv(serverfd, &c, sizeof(c), MSG_OOB) <= 0) {
            return;
        }
        got_reply = (c == 'Y'); //收到心跳，设置心跳标志
        printf("recd pong\n");
    } else if (signum == SIGALRM) {
        if (got_reply) {    //心跳标志，初始化为1
            if (send(serverfd, "?", 1, MSG_OOB) <= 0) {
                perror("send ping failed.\n");
                return;
            }
            printf("send ping\n");
            got_reply = 0;  //发送心跳之后，重置心跳标志
        } else {
            fprintf(stderr, "connection lost, reconnect\n");
            reconnect();
        }
        //unsigned int alarm(unsigned int seconds);
        //seconds秒之后发送一个SIGALARM信号给当前进程; 0表示取消该闹钟
        alarm(HEARTBEAT_INTERNAL);
    }
}
/*---------------------------------------------------------------------
    sig_handler - if the single is OOB, set flag. If ALARM, send heartbeat.
 ---------------------------------------------------------------------*/
if the single is OOB, set flag. If ALARM, send heartbeat.
if the single is OOB set flag . If ALARM send heartbeat .
void sig_handler(int signum) {
    struct tcp_info info;
    int len = sizeof(info);
    if (signum == SIGURG) {
        if (getsockopt(serverfd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t * ) & len) == 0) {
            if ((info.tcpi_state != TCP_ESTABLISHED)) {
                printf("socket is not ready. cannot ping.\n");
                return;
            }
        } else {
            printf("socket error: %s\n", strerror(errno));
            return;
        }
        char c;
        if (recv(serverfd, &c, sizeof(c), MSG_OOB) <= 0) {
            return;
        }
        got_reply = (c == 'Y'); 
        printf("recd pong\n");
    } else if (signum == SIGALRM) {
        if (got_reply) {    
            if (send(serverfd, "?", 1, MSG_OOB) <= 0) {
                perror("send ping failed.\n");
                return;
            }
            printf("send ping\n");
            got_reply = 0;  
        } else {
            fprintf(stderr, "connection lost, reconnect\n");
            reconnect();
        }
        alarm(HEARTBEAT_INTERNAL);
    }
}
void sig_handler ( int signum ) { struct tcp_info info ; int len = sizeof ( info ) ; if ( signum == SIGURG ) { if ( getsockopt ( serverfd , IPPROTO_TCP , TCP_INFO , & info , ( socklen_t * ) & len ) == 0 ) { if ( ( info . tcpi_state != TCP_ESTABLISHED ) ) { printf ( " \n " ) ; return ; } } else { printf ( " \n " , strerror ( errno ) ) ; return ; } char c ; if ( recv ( serverfd , & c , sizeof ( c ) , MSG_OOB ) <= 0 ) { return ; } got_reply = ( c == ' ' ) ; printf ( " \n " ) ; } else if ( signum == SIGALRM ) { if ( got_reply ) { if ( send ( serverfd , " " , 1 , MSG_OOB ) <= 0 ) { perror ( " \n " ) ; return ; } printf ( " \n " ) ; got_reply = 0 ; } else { fprintf ( stderr , " \n " ) ; reconnect ( ) ; } alarm ( HEARTBEAT_INTERNAL ) ; } }
sig_handler - if the single is OOB, set flag.
sig_handler - if the single is OOB set flag .
//收到心跳，设置心跳标志 //心跳标志，初始化为1 //发送心跳之后，重置心跳标志 //unsigned int alarm(unsigned int seconds); //seconds秒之后发送一个SIGALARM信号给当前进程; 0表示取消该闹钟