#include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <time.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <signal.h> const int maxline = 4096; size_t writen(int fd, const void *vptr, size_t n) { size_t nleft; size_t nwritten; const char *ptr; ptr=vptr; nleft=n; while(nleft>0) { if((nwritten=write(fd, ptr, nleft))<=0) { if(nwritten<0 && errno==EINTR) nwritten=0; else return -1; } nleft-=nwritten; ptr+=nwritten; } return n; } void str_echo(int sockfd) { size_t n; char buff[maxline]; again: while((n=read(sockfd, buff, maxline))>0) { if(writen(sockfd, buff, n)!=n) { fprintf(stderr, "writen error!\n"); } } if(n<0 && errno==EINTR) goto again; else if(n<0) { fprintf(stderr, "read error\n"); exit(-1); } } void sig_chld(int signo) { pid_t pid; int stat; while((pid=waitpid(-1, &stat, WNOHANG))>0) { printf("child %d terminated\n", pid); } return; } int main(int argc, char **argv) { int listenfd, connfd; socklen_t len; pid_t childpid; struct sockaddr_in servaddr, cliaddr; char buff[maxline], errbuff[maxline]; time_t ticks; if((listenfd=socket(AF_INET, SOCK_STREAM, 0))<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "socket error: %s\n", errbuff); exit(-1); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(9999); if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "bind error: %s\n", errbuff); exit(-1); } if(listen(listenfd, 1024)<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "listen error: %s\n", errbuff); exit(-1); } signal(SIGCHLD, sig_chld); for(;;) { len=sizeof(cliaddr); if((connfd=accept(listenfd, (struct sockaddr *)&cliaddr, &len))<0) { if(errno == EINTR) { continue; } else { strerror_r(errno, errbuff, maxline); fprintf(stderr, "accept error: %s\n", errbuff); exit(-1); } } printf("connection from %s, port %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)), ntohs(cliaddr.sin_port)); if((childpid=fork())<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "fork error: %s\n", errbuff); exit(-1); } else if(childpid == 0) { printf("ppid: %d\n", getppid()); if(close(listenfd)<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "child pid close listenfd error: %s\n", errbuff); exit(-1); } str_echo(connfd); exit(0); } else { printf("childpid: %d\n", childpid); if(close(connfd)<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "parent pid close connfd error: %s\n", errbuff); exit(-1); } } } return 0; }
标签:反射,int,char,并发,nleft,服务器,include,ptr,size From: https://www.cnblogs.com/donggongdechen/p/16740887.html