heartbeat-server.c
heartbeat-client.c
阅读(133) | 评论(0) | 转发(0) |
<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>
点击(此处)折叠或打开
- // gcc heartbeat-server.c -o heartbeat-server
- // indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *
- /* heartbeat-server.c
- *
- * Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
- * whole or in part in accordance to the General Public License (GPL).
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- /*****************************************************************************/
- /*** heartbeat-server.c ***/
- /*** ***/
- /*** Demonstrates how to keep track of the server using a "heartbeat". If ***/
- /*** the heartbeat is lost, the connection can be reestablished and the ***/
- /*** session resumed. ***/
- /*****************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <strings.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/socket.h>
- #include <resolv.h>
- #include <signal.h>
- #include <sys/wait.h>
- int client;
- struct sigaction act;
- /*---------------------------------------------------------------------
- sig_handler - catch and send heartbeat.
- ---------------------------------------------------------------------*/
- void sig_handler(int signum)
- {
- if (signum == SIGURG) {
- char c;
- recv(client, &c, sizeof(c), MSG_OOB);
- if (c == '?')
- /* Are you alive? */
- send(client, "Y", 1, MSG_OOB); /* */
- } else if (signum == SIGCHLD)
- wait(0);
- }
-
- /*---------------------------------------------------------------------
- servlet - process requests
- ---------------------------------------------------------------------*/
- void servlet(void)
- {
- int bytes;
- char buffer[1024];
- bzero(&act, sizeof(act));
- act.sa_handler = sig_handler;
- act.sa_flags = SA_RESTART;
- sigaction(SIGURG, &act, 0); /* connect SIGURG signal */
- if (fcntl(client, F_SETOWN, getpid()) != 0)
- perror("Can't claim SIGIO and SIGURG");
- do {
- bytes = recv(client, buffer, sizeof(buffer), 0);
- if (bytes > 0)
- send(client, buffer, bytes, 0);
- }
- while (bytes > 0);
- close(client);
- exit(0);
- }
-
- /*---------------------------------------------------------------------
- main - set up client and begin the heartbeat.
- ---------------------------------------------------------------------*/
- int main(int count, char *strings[])
- {
- int sd;
- struct sockaddr_in addr;
- if (count != 2) {
- printf("usage: %s <port>\n", strings[0]);
- exit(0);
- }
- bzero(&act, sizeof(act));
- act.sa_handler = sig_handler;
- act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
- if (sigaction(SIGCHLD, &act, 0) != 0)
- perror("sigaction()");
- sd = socket(PF_INET, SOCK_STREAM, 0);
- bzero(&addr, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(atoi(strings[1]));
- addr.sin_addr.s_addr = INADDR_ANY;
- if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0)
- perror("bind()");
- listen(sd, 15);
- for (;;) {
- client = accept(sd, 0, 0);
- if (client > 0) {
- if (fork() == 0) {
- close(sd);
- servlet();
- } else
- close(client);
- } else
- perror("accept()");
- }
- close(sd);
- return 0;
- }
点击(此处)折叠或打开
- // gcc heartbeat-client.c -o heartbeat-client
- // indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *
- /* heartbeat-client.c
- *
- * Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
- * whole or in part in accordance to the General Public License (GPL).
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- /*****************************************************************************/
- /*** heartbeat-client.c ***/
- /*** ***/
- /*** Demonstrates how to keep track of the server using a "heartbeat". If ***/
- /*** the heartbeat is lost, the connection can be reestablished and the ***/
- /*** session resumed. ***/
- /*****************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <resolv.h>
- #include <netinet/tcp.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
-
- #define DELAY 5 /*seconds */
- int serverfd, got_reply = 1;
-
- /*---------------------------------------------------------------------
- sig_handler - if the single is OOB, set flag. If ALARM, send heartbeat.
- ---------------------------------------------------------------------*/
- void sig_handler(int signum)
- {
- if (signum == SIGURG) {
- char c;
- recv(serverfd, &c, sizeof(c), MSG_OOB);
- got_reply = (c == 'Y'); /* Got reply */
- fprintf(stderr, "[server is alive]");
- } else if (signum == SIGALRM)
- if (got_reply) {
- send(serverfd, "?", 1, MSG_OOB); /* Alive?? */
- alarm(DELAY); /* Wait a while */
- got_reply = 0;
- } else
- fprintf(stderr, "Lost connection to server!");
- }
-
- /*---------------------------------------------------------------------
- main - set up client and begin the heartbeat.
- ---------------------------------------------------------------------*/
- int main(int count, char *strings[])
- {
- struct sockaddr_in addr;
- struct sigaction act;
- int bytes;
- char line[100];
- if (count != 3) {
- printf("usage: %s <addr> <port>\n", strings[0]);
- exit(0);
- }
- bzero(&act, sizeof(act));
- act.sa_handler = sig_handler;
- act.sa_flags = SA_RESTART;
- sigaction(SIGURG, &act, 0);
- sigaction(SIGALRM, &act, 0);
- serverfd = socket(PF_INET, SOCK_STREAM, 0);
- /*---claim SIGIO/SIGURG signals---*/
- if (fcntl(serverfd, F_SETOWN, getpid()) != 0)
- perror("Can't claim SIGURG and SIGIO");
-
- bzero(&addr, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(atoi(strings[2]));
- inet_aton(strings[1], &addr.sin_addr);
- if (connect(serverfd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
- alarm(DELAY);
- do {
- gets(line);
- printf("send [%s]\n", line);
- send(serverfd, line, strlen(line), 0);
- bytes = recv(serverfd, line, sizeof(line), 0);
- }
- while (bytes > 0);
- } else
- perror("connect failed");
- close(serverfd);
- return 0;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议