zoukankan      html  css  js  c++  java
  • 简单回射程序之处理accept返回EINTR错误的服务器程序版本

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <signal.h>
    #include <sys/socket.h>
    
    #define SERV_PORT 9999
    #define MAXLINE 4096
    #define LISTENQ 1024
    #define SA struct sockaddr
    
    typedef void Sigfunc(int);
    
    void str_echo(int);
    void sig_chld(int);
    ssize_t writen(int, const void*, size_t);
    Sigfunc *signal(int, Sigfunc*);
    
    int main(int argc, char *argv[]) {
        int listenfd, connfd;
        pid_t childpid;
        socklen_t clilen;
        struct sockaddr_in cliaddr, servaddr;
    
        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); /* Install signal processing function */
    
        for ( ; ; ) {
            clilen = sizeof(cliaddr);
           if ( (connfd = accept(listenfd, (SA *)&cliaddr, &clilen)) < 0) {
              if (errno == EINTR) {
                      continue;
              } else {
                    perror("accept");
                      return;
              }
            }
    
            if ( (childpid = fork()) == 0) { /* Generate child processes to handle foreign connections */
                close(listenfd);
                str_echo(connfd);
                exit(0);
            }
            close(connfd);
        }
        close(listenfd);
        exit(0);
    }
    
    void str_echo(int sockfd) {
        ssize_t n;
        char buf[MAXLINE];
    
        again:
        while ( (n = read(sockfd, buf, MAXLINE)) > 0) {
            writen(sockfd, buf, n);
            bzero(buf, sizeof(buf));
        }
        if (n < 0 && errno == EINTR) {
            goto again;
        } else if (n < 0) {
            perror("read");
        }
    }
     
  • 相关阅读:
    2.8
    2.7
    2.6
    2.5
    2.4第三篇读后感
    2.2第一篇读后感
    2.1
    字符统计
    6468: Snuke's Coloring
    6463: Tak and Hotels II
  • 原文地址:https://www.cnblogs.com/soldierback/p/10696320.html
Copyright © 2011-2022 走看看