C/S—心跳检测—heartbeat

来源:互联网 发布:网络剪刀手汉化 编辑:程序博客网 时间:2024/05/07 14:47
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>\n", 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>\n", 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]\n", 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) |
0

上一篇:Linux—Network—Socket—Programming—heartbeat—源代码

下一篇:accept() returns the same socket descriptor

相关热门文章
  • HTML5 音视频媒体处理相关...
  • IDC局域网IP检测脚本
  • 欢迎乱了分寸的心跳在ChinaUni...
  • 使用jquery的ajax,无法正确返...
  • 欢迎新的心跳在ChinaUnix博客...
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • linux dhcp peizhi roc
  • 关于Unix文件的软链接
  • 求教这个命令什么意思,我是新...
  • sed -e "/grep/d" 是什么意思...
  • 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
原创粉丝点击