注意:包含了“wrap.c” 和“wrap.h”文件在上篇博客中
/*** server.c ***/ #include<stdio.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> #include<signal.h> #include<sys/wait.h> #include<ctype.h> #include<unistd.h>#include"wrap.h"#define MAXLINE 8192 #define SERV_PORT 8000int main() {pid_t pid;int lfd,cfd;struct sockaddr_in serv_addr,clie_addr;socklen_t clie_addr_len;char buf[BUFSIZ];int n,i;lfd = Socket(AF_INET,SOCK_STREAM,0);bzero(&serv_addr,sizeof(serv_addr));serv_addr.sin_family = AF_INET;serv_addr.sin_port = htons(SERV_PORT);serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);//inet_pton(AF_INET,"192.168.0.1",&serv_addr.sin_addr.s_addr);Bind(lfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr));Listen(lfd,128);while(1){clie_addr_len = sizeof(clie_addr);cfd = Accept(lfd,(struct sockaddr *)&clie_addr,&clie_addr_len);pid = fork();if(pid < 0){perror("fork error");exit(1);}else if(pid == 0){close(lfd);break;}else{close(cfd);}}if(0 == pid){while(1){n = Read(cfd,buf,sizeof(buf));if( 0 == n){close(cfd);return 0;}else if(-1 == n){perror("read error") ;exit(1);}else{for(i = 0; i < n; i++) buf[i] = toupper(buf[i]);write(cfd,buf,n);}}}return 0; }
运行结果:
以上程序由于没有回收机制,所以出现僵尸进程
版本2
使用进程回收函数回收僵尸进程
/*** server.c ***/ #include<stdio.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> #include<signal.h> #include<sys/wait.h> #include<ctype.h> #include<unistd.h>#include"wrap.h"#define MAXLINE 8192 #define SERV_PORT 8000void wait_child(int signo) {while(waitpid(0,NULL,WNOHANG) > 0);return ; }int main() {pid_t pid;int lfd,cfd;struct sockaddr_in serv_addr,clie_addr;socklen_t clie_addr_len;char buf[BUFSIZ],clie_IP[BUFSIZ];int n,i;lfd = Socket(AF_INET,SOCK_STREAM,0);bzero(&serv_addr,sizeof(serv_addr));serv_addr.sin_family = AF_INET;serv_addr.sin_port = htons(SERV_PORT);serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);//inet_pton(AF_INET,"192.168.0.1",&serv_addr.sin_addr.s_addr);Bind(lfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr));Listen(lfd,128);while(1){clie_addr_len = sizeof(clie_addr);cfd = Accept(lfd,(struct sockaddr *)&clie_addr,&clie_addr_len);printf("client IP:%s,port:%d\n",inet_ntop(AF_INET,&clie_addr.sin_addr,clie_IP,sizeof(clie_IP)),ntohs(clie_addr.sin_port));pid = fork();if(pid < 0){perror("fork error");exit(1);}else if(pid == 0){close(lfd);break;}else{close(cfd);signal(SIGCHLD,wait_child);}}if(0 == pid){while(1){n = Read(cfd,buf,sizeof(buf));if( 0 == n){close(cfd);return 0;}else if(-1 == n){perror("read error") ;exit(1);}else{for(i = 0; i < n; i++) buf[i] = toupper(buf[i]);write(cfd,buf,n);write(STDOUT_FILENO,buf,n);}}}return 0; }
运行结果: