zoukankan      html  css  js  c++  java
  • C/S—心跳检测—heartbeat

    heartbeat-server.c

    点击(此处)折叠或打开

    1. // gcc heartbeat-server.c -o heartbeat-server
    2. // indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *
    3. /* heartbeat-server.c
    4.  *
    5.  * Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
    6.  * whole or in part in accordance to the General Public License (GPL).
    7.  *
    8.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    9.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    10.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    11.  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    12.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    13.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    14.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    15.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    16.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    17.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    18.  * SUCH DAMAGE.
    19. */
    20. /*****************************************************************************/
    21. /*** heartbeat-server.c ***/
    22. /*** ***/
    23. /*** Demonstrates how to keep track of the server using a "heartbeat". If ***/
    24. /*** the heartbeat is lost, the connection can be reestablished and the ***/
    25. /*** session resumed. ***/
    26. /*****************************************************************************/
    27. #include <stdio.h>
    28. #include <stdlib.h>
    29. #include <unistd.h>
    30. #include <strings.h>
    31. #include <errno.h>
    32. #include <fcntl.h>
    33. #include <sys/socket.h>
    34. #include <resolv.h>
    35. #include <signal.h>
    36. #include <sys/wait.h>
    37. int client;
    38. struct sigaction act;
    39. /*---------------------------------------------------------------------
    40.     sig_handler - catch and send heartbeat.
    41.  ---------------------------------------------------------------------*/
    42. void sig_handler(int signum)
    43. {
    44.     if (signum == SIGURG) {
    45.         char c;
    46.         recv(client, &c, sizeof(c), MSG_OOB);
    47.         if (c == '?')
    48.             /* Are you alive? */
    49.             send(client, "Y", 1, MSG_OOB);    /* */
    50.     } else if (signum == SIGCHLD)
    51.         wait(0);
    52. }

    53. /*---------------------------------------------------------------------
    54.     servlet - process requests
    55.  ---------------------------------------------------------------------*/
    56. void servlet(void)
    57. {
    58.     int bytes;
    59.     char buffer[1024];
    60.     bzero(&act, sizeof(act));
    61.     act.sa_handler = sig_handler;
    62.     act.sa_flags = SA_RESTART;
    63.     sigaction(SIGURG, &act, 0);    /* connect SIGURG signal */
    64.     if (fcntl(client, F_SETOWN, getpid()) != 0)
    65.         perror("Can't claim SIGIO and SIGURG");
    66.     do {
    67.         bytes = recv(client, buffer, sizeof(buffer), 0);
    68.         if (bytes > 0)
    69.             send(client, buffer, bytes, 0);
    70.     }
    71.     while (bytes > 0);
    72.     close(client);
    73.     exit(0);
    74. }

    75. /*---------------------------------------------------------------------
    76.     main - set up client and begin the heartbeat.
    77.  ---------------------------------------------------------------------*/
    78. int main(int count, char *strings[])
    79. {
    80.     int sd;
    81.     struct sockaddr_in addr;
    82.     if (count != 2) {
    83.         printf("usage: %s <port> ", strings[0]);
    84.         exit(0);
    85.     }
    86.     bzero(&act, sizeof(act));
    87.     act.sa_handler = sig_handler;
    88.     act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
    89.     if (sigaction(SIGCHLD, &act, 0) != 0)
    90.         perror("sigaction()");
    91.     sd = socket(PF_INET, SOCK_STREAM, 0);
    92.     bzero(&addr, sizeof(addr));
    93.     addr.sin_family = AF_INET;
    94.     addr.sin_port = htons(atoi(strings[1]));
    95.     addr.sin_addr.s_addr = INADDR_ANY;
    96.     if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
    97.         perror("bind()");
    98.     listen(sd, 15);
    99.     for (;;) {
    100.         client = accept(sd, 0, 0);
    101.         if (client > 0) {
    102.             if (fork() == 0) {
    103.                 close(sd);
    104.                 servlet();
    105.             } else
    106.                 close(client);
    107.         } else
    108.             perror("accept()");
    109.     }
    110.     close(sd);
    111.     return 0;
    112. }
    heartbeat-client.c

    点击(此处)折叠或打开

    1. // gcc heartbeat-client.c -o heartbeat-client
    2. // indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *
    3. /* heartbeat-client.c
    4.  *
    5.  * Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
    6.  * whole or in part in accordance to the General Public License (GPL).
    7.  *
    8.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    9.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    10.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    11.  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    12.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    13.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    14.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    15.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    16.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    17.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    18.  * SUCH DAMAGE.
    19. */
    20. /*****************************************************************************/
    21. /*** heartbeat-client.c ***/
    22. /*** ***/
    23. /*** Demonstrates how to keep track of the server using a "heartbeat". If ***/
    24. /*** the heartbeat is lost, the connection can be reestablished and the ***/
    25. /*** session resumed. ***/
    26. /*****************************************************************************/
    27. #include <stdio.h>
    28. #include <stdlib.h>
    29. #include <unistd.h>
    30. #include <string.h>
    31. #include <errno.h>
    32. #include <fcntl.h>
    33. #include <signal.h>
    34. #include <resolv.h>
    35. #include <netinet/tcp.h>
    36. #include <netinet/in.h>
    37. #include <arpa/inet.h>
    38. #include <sys/socket.h>

    39. #define DELAY 5            /*seconds */
    40. int serverfd, got_reply = 1;

    41. /*---------------------------------------------------------------------
    42.     sig_handler - if the single is OOB, set flag. If ALARM, send heartbeat.
    43.  ---------------------------------------------------------------------*/
    44. void sig_handler(int signum)
    45. {
    46.     if (signum == SIGURG) {
    47.         char c;
    48.         recv(serverfd, &c, sizeof(c), MSG_OOB);
    49.         got_reply = (c == 'Y');    /* Got reply */
    50.         fprintf(stderr, "[server is alive]");
    51.     } else if (signum == SIGALRM)
    52.         if (got_reply) {
    53.             send(serverfd, "?", 1, MSG_OOB);    /* Alive?? */
    54.             alarm(DELAY);    /* Wait a while */
    55.             got_reply = 0;
    56.         } else
    57.             fprintf(stderr, "Lost connection to server!");
    58. }

    59. /*---------------------------------------------------------------------
    60.     main - set up client and begin the heartbeat.
    61.  ---------------------------------------------------------------------*/
    62. int main(int count, char *strings[])
    63. {
    64.     struct sockaddr_in addr;
    65.     struct sigaction act;
    66.     int bytes;
    67.     char line[100];
    68.     if (count != 3) {
    69.         printf("usage: %s <addr> <port> ", strings[0]);
    70.         exit(0);
    71.     }
    72.     bzero(&act, sizeof(act));
    73.     act.sa_handler = sig_handler;
    74.     act.sa_flags = SA_RESTART;
    75.     sigaction(SIGURG, &act, 0);
    76.     sigaction(SIGALRM, &act, 0);
    77.     serverfd = socket(PF_INET, SOCK_STREAM, 0);
    78.     /*---claim SIGIO/SIGURG signals---*/
    79.     if (fcntl(serverfd, F_SETOWN, getpid()) != 0)
    80.         perror("Can't claim SIGURG and SIGIO");

    81.     bzero(&addr, sizeof(addr));
    82.     addr.sin_family = AF_INET;
    83.     addr.sin_port = htons(atoi(strings[2]));
    84.     inet_aton(strings[1], &addr.sin_addr);
    85.     if (connect(serverfd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
    86.         alarm(DELAY);
    87.         do {
    88.             gets(line);
    89.             printf("send [%s] ", line);
    90.             send(serverfd, line, strlen(line), 0);
    91.             bytes = recv(serverfd, line, sizeof(line), 0);
    92.         }
    93.         while (bytes > 0);
    94.     } else
    95.         perror("connect failed");
    96.     close(serverfd);
    97.     return 0;
    98. }






    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
    阅读(133) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    使用指针的误区之指针未初始化
    实验室react项目名词解释
    生活感悟之大学
    git 快速入门
    口才锻炼
    narcissus
    crest value &minimum
    factorial
    Str_turn
    array_x
  • 原文地址:https://www.cnblogs.com/ztguang/p/12649386.html
Copyright © 2011-2022 走看看