转载:/li_wen01/article/details/52685844
前面已讲过使用一个进程实现服务端和客户端P2P通信的实例,但是它只能同时处理一个客户端的连接。如果要实现并发处理多个客户端的连接并且实现P2P通信,可以使用多进程来处理。相比与多线程来说,多进程耗费的系统资源是比较多的,后续会介绍使用线程池实现简单的数据收发。
使用多进程并发处理多个client请求以及实现P2P通信,父进程专门监听端口,每监听到一个连接就创建一个子进程处理这个客户端,于此同时,在子进程中创建一个孙子进程来处理数据的读取,在子进程实现数据的发送。如果客户端断开连接,recv函数会返回参数0,recv函数所在进程发送信号给send函数所在进程,然后退出recv进程,send函数所在进程接收到信号SIGUSR1就退出该进程。在多进程中,子进程退出时会产生僵尸进程,僵尸进程的处理有多种方法,最简单的就是直接忽视SIGCHLD信号。
下面直接上代码:
#include<stdio.h>#include<sys/types.h>#include<sys/socket.h>#include<unistd.h>#include<stdlib.h>#include<errno.h>#include<arpa/inet.h>#include<netinet/in.h>#include<string.h>#include<signal.h>#define MAXLINE 256#define PORT6666/*进程退出函数*/void process_out(int signo){exit(EXIT_SUCCESS);}/*socket write 函数*/void write_func(int pid, int fd){char* write = "I am server";printf("write id = %d\n",pid);signal(SIGUSR1,process_out); /* 注册信号SIGUSR1,该信号由read 进程发送过来。*/while(1){sleep(1);send(fd,write,strlen(write)+1,0);}}/*socket read 函数*/void read_func(int pid, int fd){char readbuff[MAXLINE];int n = 0;printf("read id = %d \n",pid);memset(&readbuff,0,sizeof(readbuff));while(1){n = recv(fd, readbuff, MAXLINE, 0); /*recv 在这里是阻塞运行*/if(n > 0) /*客户端有数据发送过来*/{printf("server recv data: %s \n",readbuff);}else if(n == 0)/*客户端断开了连接*/{break;}};kill(pid, SIGUSR1); /*发送信号SIGUSR1 到write进程*/exit(EXIT_SUCCESS); /*进程退出*/}int main(void){int listenfd,connetfd;int on = 1;int addrlen = 0;pid_t pid, pid_child, pid_send;struct sockaddr_in server_addr;struct sockaddr_in client_addr;if ((listenfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){printf("create socket err \n");}/*设置服务端地址*/addrlen = sizeof(struct sockaddr_in);memset(&server_addr, 0, addrlen);server_addr.sin_family = AF_INET; /*AF_INET表示 IPv4 Intern 协议*/server_addr.sin_addr.s_addr = htonl(INADDR_ANY); /*INADDR_ANY 可以监听任意IP */server_addr.sin_port = htons(PORT); /*设置端口*//*对套接字进行设置*/if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);exit(0);}/*绑定地址结构到套接字描述符*/if( bind(listenfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1){printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);exit(0);}/*设置监听队列,这里设置为10,表示可以同时处理10个客户端的连接*/if( listen(listenfd, 10) == -1){printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);exit(0);}printf("wait client accpt \n");while(1){/*接收客户端的连接,这里会阻塞,直到有客户端连接*/if( (connetfd = accept(listenfd, (struct sockaddr*)&client_addr, &addrlen)) == -1){printf("accept socket error: %s(errno: %d)",strerror(errno),errno);continue;}signal(SIGCHLD, SIG_IGN); /*忽略SIGCHLD,避免僵尸进程*/pid = fork();if(pid == -1){printf("fork err \n");}if(pid == 0)/* 子进程*/{pid_child = fork(); if(pid_child == 0) /*孙子进程*/{pid_send = getpid(); /*获取孙子进程ID*/read_func(pid_send, connetfd);}else{pid_send = getpid(); /* 获取子进程ID*/write_func(pid_send,connetfd);}}}}
测试程序这里不再实现,将上面代码在PC机上编译运行,在手机端使用网络助手工具直接连接PC机的6666端口,可以看到如下运行结果:
root@ubuntu:/home/share/Socket/process# ./process_serverwait client accpt write id = 3883read id = 3884 server recv data: I am client 1 server recv data: I am client 1 server recv data: I am client 1 server recv data: I am client 1 write id = 3885read id = 3886 server recv data: I am client 1 server recv data: I am client 2 server recv data: I am client 1 server recv data: I am client 2 server recv data: I am client 1 write id = 3887read id = 3888 server recv data: I am client 2 server recv data: I am client 1 server recv data: I am client 2 server recv data: I am client 1 server recv data: I am client 2 server recv data: I am client 1 server recv data: I am client 1 server recv data: I am client 1 ^Croot@ubuntu:/home/share/Socket/process#
先连接三个客户端,然后再断开连接,客户端收到的数据不再贴出。于此同时我们使用PS查看进程状态:
root@ubuntu:/home/share# ps aux |grep process_serverroot 3882 0.0 0.0 4164 348 pts/0 S+ 22:38 0:00 ./process_serverroot 3883 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3884 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3885 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3886 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3887 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3888 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3890 0.0 0.0 13588 940 pts/2 S+ 22:38 0:00 grep --color=auto process_serverroot@ubuntu:/home/share# ps aux |grep process_serverroot 3882 0.0 0.0 4164 348 pts/0 S+ 22:38 0:00 ./process_serverroot 3883 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3884 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3885 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3886 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3894 0.0 0.0 13588 940 pts/2 S+ 22:38 0:00 grep --color=auto process_serverroot@ubuntu:/home/share# ps aux |grep process_serverroot 3882 0.0 0.0 4164 348 pts/0 S+ 22:38 0:00 ./process_serverroot 3883 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3884 0.0 0.0 4164 88 pts/0 S+ 22:38 0:00 ./process_serverroot 3896 0.0 0.0 13588 940 pts/2 S+ 22:39 0:00 grep --color=auto process_serverroot@ubuntu:/home/share# ps aux |grep process_serverroot 3882 0.0 0.0 4164 348 pts/0 S+ 22:38 0:00 ./process_serverroot 3898 0.0 0.0 13588 944 pts/2 S+ 22:39 0:00 grep --color=auto process_serverroot@ubuntu:/home/share#
可以看到并未产生僵尸进程,但是如果把上面程序的signal(SIGCHLD, SIG_IGN);去掉,我们就可以看到产生了僵尸进程。
root@ubuntu:/home/ysj000# ps aux |grep process_serverroot 3812 0.0 0.0 4164 352 pts/0 S+ 22:24 0:00 ./process_serverroot 3814 0.0 0.0 4164 92 pts/0 S+ 22:25 0:00 ./process_serverroot 3815 0.0 0.0 4164 92 pts/0 S+ 22:25 0:00 ./process_serverroot 3816 0.0 0.0 4164 92 pts/0 S+ 22:25 0:00 ./process_serverroot 3817 0.0 0.0 4164 92 pts/0 S+ 22:25 0:00 ./process_serverroot 3819 0.0 0.0 13588 944 pts/2 S+ 22:25 0:00 grep --color=auto process_serverroot@ubuntu:/home/ysj000# ps aux |grep process_serverroot 3812 0.0 0.0 4164 352 pts/0 S+ 22:24 0:00 ./process_serverroot 3814 0.0 0.0 4164 92 pts/0 S+ 22:25 0:00 ./process_serverroot 3815 0.0 0.0 4164 92 pts/0 S+ 22:25 0:00 ./process_serverroot 3816 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3821 0.0 0.0 13588 944 pts/2 S+ 22:25 0:00 grep --color=auto process_serverroot@ubuntu:/home/ysj000# ps aux |grep process_serverroot 3812 0.0 0.0 4164 352 pts/0 S+ 22:24 0:00 ./process_serverroot 3814 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3816 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3823 0.0 0.0 13588 944 pts/2 S+ 22:26 0:00 grep --color=auto process_serverroot@ubuntu:/home/ysj000# ps aux |grep process_serverroot 3812 0.0 0.0 4164 352 pts/0 S+ 22:24 0:00 ./process_serverroot 3814 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3816 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3824 0.0 0.0 4164 92 pts/0 S+ 22:26 0:00 ./process_serverroot 3825 0.0 0.0 4164 92 pts/0 S+ 22:26 0:00 ./process_serverroot 3827 0.0 0.0 13588 944 pts/2 S+ 22:26 0:00 grep --color=auto process_serverroot@ubuntu:/home/ysj000# ps aux |grep process_serverroot 3812 0.0 0.0 4164 352 pts/0 S+ 22:24 0:00 ./process_serverroot 3814 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3816 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3824 0.0 0.0 4164 92 pts/0 S+ 22:26 0:00 ./process_serverroot 3825 0.0 0.0 4164 92 pts/0 S+ 22:26 0:00 ./process_serverroot 3830 0.0 0.0 4164 92 pts/0 S+ 22:27 0:00 ./process_serverroot 3831 0.0 0.0 4164 92 pts/0 S+ 22:27 0:00 ./process_serverroot 3833 0.0 0.0 13588 940 pts/2 S+ 22:27 0:00 grep --color=auto process_serverroot@ubuntu:/home/ysj000# ps aux |grep process_serverroot 3812 0.0 0.0 4164 352 pts/0 S+ 22:24 0:00 ./process_serverroot 3814 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3816 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3824 0.0 0.0 4164 92 pts/0 S+ 22:26 0:00 ./process_serverroot 3825 0.0 0.0 4164 92 pts/0 S+ 22:26 0:00 ./process_serverroot 3830 0.0 0.000 pts/0 Z+ 22:27 0:00 [process_server] <defunct>root 3835 0.0 0.0 13588 944 pts/2 S+ 22:27 0:00 grep --color=auto process_serverroot@ubuntu:/home/ysj000# ps aux |grep process_serverroot 3812 0.0 0.0 4164 352 pts/0 S+ 22:24 0:00 ./process_serverroot 3814 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3816 0.0 0.000 pts/0 Z+ 22:25 0:00 [process_server] <defunct>root 3824 0.0 0.000 pts/0 Z+ 22:26 0:00 [process_server] <defunct>root 3830 0.0 0.000 pts/0 Z+ 22:27 0:00 [process_server] <defunct>root 3839 0.0 0.0 13588 944 pts/2 S+ 22:27 0:00 grep --color=auto process_server
注意1:在上面的代码中,我们直接忽视了SIGCHLD 信号来避免产生僵尸进程,在linux系统中是可以的,但是在其他的一些系统不一定都可以。另外,直接忽视SIGCHLD信号会造成一些其他的影响:会影响system函数的正常使用。system里面会将sigchld设置为阻塞,因为system里面会调用fork,然后执行命令,最后通过waitpid等待子进程的返回,不将sigchld设置为阻塞有可能信号被别人处理掉,system无法获得到信号就会报错,错误号echld,no child processes,这就是因为信号被别人处理了,其实命令是运行成功的。而忽略sigchld信号就会导致这一问题。所以正常使用的时候需要多加注意(可以使用wait 或waitpid 来避免僵尸进程)如下:
void sig_chld(int signo){pid_tpid;int stat;while ((pid = waitpid(-1,stat,WNOHANG))>0){//printf("child %d terminated \n",pid);}return ;}
信号注册 signal(SIGCHLD, sig_chld);
注意2:如果我们不主动收发数据也想检测到TCP连接的对方已经退出或是崩溃,我们可以使用套接字选项SO_KEEPALIVE来实现。