使用到的函数:

// 子进程返回0,父进程返回子进程ID,出错返回-1
pid_t fork(void);
pid_t wait(int *wstatus);
// 最常用的option是WNOHANG,它告知内核在没有已终止子进程时不要阻塞
pid_t waitpid(pid_t pid, int *wstatus, int options);

服务器程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define MAXLINE 4096
#define LISTENQ 10

void doEcho(int sockfd) {
    char buff[MAXLINE];
    while (true) {
        memset(buff, 0, sizeof(buff));
        int n = read(sockfd, buff, MAXLINE);
        if (n < 0) {
            perror("read error");
            exit(1);
        } else if (n == 0) {
            printf("client closed\n");
            break;
        }
        fputs(buff, stdout);
        write(sockfd, buff, n);
    }
}

int main(int argc, char **argv) {
    
    int listenfd, connfd;
    pid_t childpid;
    socklen_t clilen;
    struct sockaddr_in servaddr, cliaddr;
    
    if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket error");
        exit(1);
    }
    
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(13);  /* daytime server */
    
    if ( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
        perror("bind error");
        exit(1);
    }
    
    if ( listen(listenfd, LISTENQ) < 0) {
        perror("listen error");
        exit(1);
    }
    
    for ( ; ; ) {
        clilen = sizeof(cliaddr);
        if ( (connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen)) < 0) {
            perror("accept error");
            exit(1);
        }
        /* 子进程 */
        if ( (childpid = fork()) == 0) {
            close(listenfd);
            // 回射程序
            doEcho(connfd);
            exit(0);
        }
        /* 父进程 */
        close(connfd);
    }
}
View Code

相关文章: