心跳处理
发布日期:2022-04-11 11:39:13
浏览次数:44
分类:博客文章
本文共 2918 字,大约阅读时间需要 9 分钟。
#include <sys/types.h> /* basic system data types */ #include <sys/socket.h> /* basic socket definitions */ #if TIME_WITH_SYS_TIME #include <sys/time.h> /* timeval{} for select() */ #include <time.h> /* timespec{} for pselect() */ #else #if HAVE_SYS_TIME_H #include <sys/time.h> /* includes <time.h> unsafely */ #else #include <time.h> /* old system? */ #endif #endif #include <netinet/ in.h> /* sockaddr_in{} and other Internet defns */ #include <arpa/inet.h> /* inet(3) functions */ #include <errno.h> #include <fcntl.h> /* for nonblocking */ #include <netdb.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include < string.h> #include <sys/stat.h> /* for S_xxx file mode constants */ #include <sys/uio.h> /* for iovec{} and readv/writev */ #include <unistd.h> #include <sys/wait.h> #define SERV_PORT 9877 #define LISTENQ 100 #define MAXLINE 512 static int servfd; static int nsec; static int maxnalarms; static int nprobes; static void sig_urg( int),sig_alrm( int); void heartbeat_serv( int servfd_arg, int nsec_arg, int maxnalarms_arg) { servfd = servfd_arg; if((nsec = nsec_arg) < 1) nsec = 1; if((maxnalarms = maxnalarms_arg) < nsec) maxnalarms = nsec; signal(SIGURG, sig_urg); // SIGURG ---- 当out-of-band data接收的时候可能发送 fcntl(servfd, F_SETOWN, getpid()); signal(SIGALRM, sig_alrm); // SIGALRM ----- 用alarm函数设置的timer超时或setitimer函数设置的interval timer超时 alarm(nsec); } void err_sys( const char *error_string) { printf( " %s\n ", error_string); } static void sig_urg( int signo) { int n; char c; if(( n = recv(servfd, &c, 1, MSG_OOB)) < 0 ) { if(errno != EWOULDBLOCK) err_sys( " recv error "); } send(servfd, &c, 1, MSG_OOB); nprobes = 0; return; } static void sig_alrm ( int signo) { if(++nprobes > maxnalarms) { printf( " no probs from client\n "); exit( 0); } alarm(nsec); return; } void str_echo( int sockfd) { ssize_t n; char line[MAXLINE]; heartbeat_serv(sockfd, 1 , 5); for(;;) { if(( n = read(sockfd, line, MAXLINE)) == 0) { return; // connection closed by other end } write(sockfd, line, n); } } void sig_chld( int signo) { pid_t pid; int stat; while( (pid = waitpid(- 1, &stat, WNOHANG)) > 0 ) { printf( " child %d terminated \n ", pid); } return; } #define SA struct sockaddr int main( int argc, char **argv) { int listenfd, connfd; pid_t childpid; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; void sig_chld( int); listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); bind(listenfd, (SA*)&servaddr, sizeof(servaddr)); listen(listenfd, LISTENQ); signal(SIGCHLD, sig_chld); for(;;) { clilen = sizeof(cliaddr); if((connfd = accept(listenfd, (SA*)&cliaddr, &clilen )) < 0) { if(errno == EINTR) continue; else err_sys( " accept error "); } if((childpid = fork() ) == 0) { close(listenfd); str_echo(connfd); exit( 0); } close(connfd); } // for }
转载地址:https://www.cnblogs.com/no7dw/archive/2012/09/01/2666766.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月08日 11时12分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
dp46上 HDU2084
2019-04-26
dp46上 HDU1421
2019-04-26
UESTC 1324线段树
2019-04-26
POJ1651 区间dp
2019-04-26
spfa、Dijkstra、Floyd算法最短路算法详解
2019-04-26
HDU4725(spfa+双端队列优化)
2019-04-26
PowerOj 2392(树状数组 or CDQ分治)
2019-04-26
HDU 6119(区间交叉问题)
2019-04-26
hdu 6143(精妙的递推)
2019-04-26
数位dp
2019-04-26
Power oj 2540 (FFT卷积)
2019-04-26
hdu 6165(dfs or bfs or tarjan+topsort)
2019-04-26
hdu 6168(stl)
2019-04-26
hdu 6170(正则表达式)
2019-04-26
排列组合 "n个球放入m个盒子m"问题 总结(转)
2019-04-26
codeforces845C(stl)
2019-04-26
图的几种存储方式(邻接矩阵+邻接表+vector)
2019-04-26
[LeetCode] 67. 二进制求和(简单模拟二进制求和)
2019-04-26
HDU1233(基础最小生成树 prim和 kruskal)
2019-04-26