Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6684582
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: C/C++

2015-12-19 22:17:04

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 \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 \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. }






阅读(1595) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~