C/S—心跳检测—heartbeat
来源:互联网 发布:网络剪刀手汉化 编辑:程序博客网 时间:2024/05/07 14:47
heartbeat-server.c
heartbeat-client.c
点击(此处)折叠或打开
- // 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;
- }
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...
给主人留下些什么吧!~~
评论热议
0 0
- C/S—心跳检测—heartbeat
- (OK) C/S—心跳检测—heartbeat
- (OK) C/S—心跳检测—heartbeat
- 简单C/S系统(线程池+心跳检测+多线程+滚动日志)
- heartbeat 心跳技术
- [heartbeat]基于httpclient的文件可配置的心跳检测应用
- Hadoop-2.4.1源码分析--HDFS HeartBeat(心跳检测)整体结构
- Hadoop-2.4.1源码分析--HDFS HeartBeat(心跳检测)数据结构初始化
- Hadoop-2.4.1源码分析--HDFS HeartBeat(心跳检测)之BPServiceActor工作线程运行流程(上)
- Hadoop-2.4.1源码分析--HDFS HeartBeat(心跳检测)之BPServiceActor工作线程运行流程(下)
- Hadoop-2.4.1源码分析--HDFS HeartBeat(心跳检测)之DataNode端数据块增量汇报
- 心跳检测
- 心跳检测
- 心跳检测
- 心跳检测
- 心跳检测
- 心跳检测
- 心跳检测
- JS 事件基础 创建滚动条
- nginx location匹配规则
- CentOS 7 - The perfect desktop guide
- 阶乘之和
- Linux—Network—Socket—Programming—heartbeat—源代码
- C/S—心跳检测—heartbeat
- 分析Servlet、Filter、Interception和Listener
- accept() returns the same socket descriptor
- 如何判断SOCKET已经断开
- 使用java 和freemarker实现多级菜单(分类)
- Lua环境安装
- (OK) C/S—心跳检测—heartbeat
- java中的缓存技术该如何实现
- 遗产
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
战锤末世鼠疫
战锤巨人
战锤之黑暗千年
寒骨战锤
驰骋在战锤世界
战锤神座sodu
战锤末世鼠疫2
战锤全面战争15个派系
战锤之混沌的复兴
战锤40k堕落修女
战锤40k之远东风暴
战锤神座txt下载
战锤神座 汉朝天子
战锤末世鼠疫2职业
战锤2全面战争闪退
2018战锤2凡事帝国适合新手的
战锤2全面战争吸血鬼海岸
战锤任务
战锤末日鼠疫2
战锤蜥蜴人
战锤图片
战锤online
战锤世界观
战锤40000
战镰
酒神的战镰
镰鼬
镰
镰鼬哪里多
叶选镰
镰仓旅游
镰组词
镰怎么读
日本镰仓
深渊之镰无措仓惶
深渊之镰
钩镰枪
骨镰套装
火镰
七镰星作品
暗黑破坏神3夺魂之镰