首页 > 代码库 > TCP编程:多进程(fork)并发处理客户端请求

TCP编程:多进程(fork)并发处理客户端请求

忽略SIGCHLD信号,交给内核处理子进程,避免产生僵尸进程。

服务器端:

 1 #include <iostream>
 2 #include <netinet/in.h>
 3 #include <cstring>
 4 #include <unistd.h>
 5 #include<signal.h>
 6 
 7 #define MAXLINE 4096
 8 
 9 int main(int argc,char ** argv) {
10     
11     signal(SIGCHLD, SIG_IGN);
12     int listenfd,connfd;
13     struct sockaddr_in servaddr;
14     char buff[4096];
15     int n;
16 
17     if((listenfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
18         printf("create socket error:%s(errno: %d)\n",strerror(errno),errno);
19         return 0;
20     }
21 
22     memset(&servaddr,0,sizeof(servaddr));
23     servaddr.sin_family = AF_INET;
24     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
25     servaddr.sin_port = htonl(6666);
26 
27     if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) == -1){
28         printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
29         return 0;
30     }
31 
32     if(listen(listenfd,10) == -1){
33         printf("listen socket error:%s(errno: %d)\n",strerror(errno),errno);
34         return 0;
35     }
36 
37     printf("======waiting for client‘s request======\n");
38     while(1){
39 
40         if((connfd = accept(listenfd,(struct sockaddr*)NULL,NULL)) == -1){
41 
42             printf("accept socket error:%s(errno:%d)\n",strerror(errno),errno);
43             continue;
44 
45         }
46          pid = fork();
47         if (pid == -1)
48             ERR_EXIT("fork error");
49         if (pid == 0)
50         {
51             
52             close(listenfd);
53             while(1){
54                     n = recv(connfd,buff,MAXLINE,0);
55                     buff[n] = \0;
56                     printf("recv msg from client:%s\n",buff);
57                     close(connfd);
58             }
59             exit(EXIT_SUCCESS);
60         }
61         else
62             close(conn); 
63     }
64 
65     return 0;
66 }
67        

客户端:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<errno.h>
 5 #include<sys/types.h>
 6 #include<sys/socket.h>
 7 #include<netinet/in.h>
 8 #include<arpa/inet.h>
 9 #include<unistd.h>
10 #define MAXLINE 4096
11 
12 int main(int argc, char** argv){
13     int   sockfd, n;
14     char  recvline[4096], sendline[4096];
15     struct sockaddr_in  servaddr;
16 
17     if( argc != 2){
18         printf("usage: ./client <ipaddress>\n");
19         return 0;
20     }
21 
22     if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
23         printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
24         return 0;
25     }
26 
27     memset(&servaddr, 0, sizeof(servaddr));
28     servaddr.sin_family = AF_INET;
29     servaddr.sin_port = htons(6666);
30     if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
31         printf("inet_pton error for %s\n",argv[1]);
32         return 0;
33     }
34 
35     if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
36         printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
37         return 0;
38     }
39 
40     printf("send msg to server: \n");
41     fgets(sendline, 4096, stdin);
42     if( send(sockfd, sendline, strlen(sendline), 0) < 0){
43         printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
44         return 0;
45     }
46     close(sockfd);
47     return 0;
48 }

 技术分享

TCP编程:多进程(fork)并发处理客户端请求