Unix/Linux守护进程服务器示例

服务端代码:

#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/wait.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MAXFD 100
#define LISTENQ 100
#define BUFSIZE 4096
void sig_child(int signo) {
    int ppid;
    while ((ppid = waitpid(-1,NULL,WNOHANG)) > 0) {
        syslog(LOG_NOTICE,"child process number: %d terminated",ppid);
    }
}

void server_echo(int fd) {
    char buf[BUFSIZE];
    int n;
    time_t t = time(NULL);
    strftime(buf,BUFSIZE,"%Y %x %X",localtime(&t));
    int len = strlen(buf);
    buf[len - 1] = '\n';
    buf[len] = '\0';
    if (write(fd,buf,strlen(buf)) != strlen(buf)) {
        syslog(LOG_ERR,"pid: %d write error: %s\n",getpid(),strerror(errno));
        exit(1);
    }
}
int main(int argc,char **argv) {
    if (argc != 2) {
        printf("please add <service name or port\n");
        exit(1);
    }

    int pid;
    if ((pid = fork()) < 0) {
        printf("fork error: %s\n",strerror(errno));
        exit(1);
    }else if (pid) {
        exit(0);
    }

    if (setsid() < 0) {
        printf("setsid error: %s\n",strerror(errno));
        exit(1);
    }

    if (signal(SIGHUP,SIG_IGN) == SIG_ERR) {
        syslog(LOG_ERR,"pid: %d fork error: %s\n",getpid(),strerror(errno));
        exit(1);
    }
    umask(0);
    if ((pid = fork()) < 0) {
        syslog(LOG_ERR,"pid: %d fork error: %s\n",getpid(),strerror(errno));
        exit(1);
    }else if (pid) {
        exit(0);
    }

    chdir("/");
    for (int i = 0; i < MAXFD; ++i) {
        close(i);
    }
    open("/dev/null",O_RDONLY);
    open("/dev/null",O_RDWR);
    open("/dev/null",O_RDWR);

    struct addrinfo hints;
    bzero(&hints,sizeof(struct addrinfo));

    hints.ai_flags = AI_PASSIVE;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    struct addrinfo* results;
    int err;
    if ((err = getaddrinfo(NULL,argv[1],&hints,&results)) != 0) {
        syslog(LOG_ERR,"pid: %d getaddrinfo error: %s\n",getpid(),gai_strerror(err));
        exit(1);
    }
    int sockfd;
    struct addrinfo* dummy = results;
    for (; dummy != NULL; dummy = dummy->ai_next) {
        if ((sockfd = socket(dummy->ai_family,dummy->ai_socktype,dummy->ai_protocol)) < 0) {
            continue;
        }

        if (bind(sockfd,dummy->ai_addr,dummy->ai_addrlen) == 0) {
            break;
        }

        close(sockfd);
    }
    if (dummy == NULL) {
        freeaddrinfo(results);
        syslog(LOG_NOTICE,"pid: %d all connect failed\n",getpid());
        exit(1);
    }
    freeaddrinfo(results);
    if (listen(sockfd,LISTENQ) < 0) {
        syslog(LOG_ERR,"pid: %d listen error: %s\n",getpid(),strerror(errno));
        exit(1);
    }
    if (signal(SIGCHLD,sig_child) == SIG_ERR) {
        syslog(LOG_ERR,"pid: %d signal error: %s\n",getpid(),strerror(errno));
        exit(1);
    }
    int connfd;
    for (; ;) {
        errno = 0;
        if ((connfd = accept(sockfd,NULL,NULL)) < 0) {
            if (errno == EINTR) {
                continue;
            }
            syslog(LOG_ERR,"pid:%d accept error: %s\n",getpid(),strerror(errno));
            exit(1); 
        }
        if ((pid = fork()) < 0) {
            syslog(LOG_ERR,"pid:%d fork error: %s\n",getpid(),strerror(errno));
            exit(1);
        }else if (pid == 0) {
            close(sockfd);
            server_echo(connfd);
            close(connfd);
            exit(0);
        }
        close(connfd);
    }
    return 0;
}

客户端代码:

#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

#define BUFSIZE 4096

int main(int argc,char **argv) {
    if (argc != 3) {
        printf("please add <ip address or host name> <service name or port>\n");
        exit(1);
    }

    struct addrinfo hints;
    bzero(&hints,sizeof(struct addrinfo));

    hints.ai_flags = AI_ALL;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    struct addrinfo* results;
    int err;

    if ((err = getaddrinfo(argv[1],argv[2],&hints,&results)) != 0) {
        printf("getaddrinfor error: %s\n",gai_strerror(err));
        exit(1);
    }
    struct addrinfo* dummy = results;
    int sockfd;
    for (; dummy != NULL; dummy = dummy->ai_next) {
        if ((sockfd = socket(dummy->ai_family,dummy->ai_socktype,dummy->ai_protocol)) < 0) {
            continue;
        }

        if (connect(sockfd,dummy->ai_addr,dummy->ai_addrlen) == 0) {
            break;
        }
        close(sockfd);
    }

    char recv[BUFSIZE];
    int n;

    while ((n = read(sockfd,recv,BUFSIZE)) > 0) {
        if (write(STDOUT_FILENO,recv,n) != n) {
            printf("write error: %s\n",strerror(errno));
            exit(1);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值