In the last blog, we implemented a single-process version of a simple TCP network program. This program can only connect to one client at a time, which has limitations. So now let's implement a multi-process version of a simple TCP network program that supports multiple connections by creating child processes.
server.c:
#include <stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<unistd.h> #include<string.h> #include<sys/types.h> #include<arpa/inet.h> #include<stdlib.h> #define MAX 128 int Startup(char* ip,int port){ int sock = socket (AF_INET, SOCK_STREAM, 0); if(sock < 0){ printf("socket error!\n"); exit(2); } struct sockaddr_in local; local.sin_family = AF_INET; local.sin_addr.s_addr = inet_addr(ip); local.sin_port = htons(port); if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0){ printf("bind error!\n"); exit(3); } if(listen(sock,5) < 0){ printf("listen error!\n"); exit(4); } return sock; } void service(int sock,char* ip,int port){ char buf[MAX]; while(1){ buf[0] = 0; ssize_t s = read(sock,buf,sizeof(buf)-1); if(s > 0){ buf[s] = 0; printf("[%s:%d] say# %s\n",ip,port,buf); write(sock,buf,strlen(buf)); } else if(s == 0){ printf("client [%s:%d] quit!\n",ip,port); break; } else{ printf("read error!\n"); break; } } } int main(int argc,char* argv[]){ if(argc != 3){ printf("Usage:%s [ip] [port]\n",argv[0]); return 1; } int listen_sock = Startup(argv[1],atoi(argv[2])); struct sockaddr_in peer; char ipBuf[24]; for(;;){ ipBuf[0] = 0; socklen_t len = sizeof(peer); int new_sock = accept(listen_sock,(struct sockaddr*)&peer,&len); if(new_sock < 0){ printf("accept error!\n"); continue; } inet_ntop(AF_INET,(const void*)&peer.sin_addr,ipBuf,sizeof(ipBuf)); int p = ntohs(peer.sin_port); printf("get a new connect,[%s:%d]\n",ipBuf,p); pid_t id = fork(); if(id == 0){//child close(listen_sock); if(fork() > 0){ exit(0); } service(new_sock,ipBuf,p); close(new_sock); exit(0); } else if(id > 0){//father close(new_sock); waitpid(id,NULL,0); } else{ printf("fork error!\n"); continue; } } return 0; }
client.c:
#include <stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<unistd.h> #include<string.h> #include<sys/types.h> #include<stdlib.h> #include<string.h> #define MAX 128 int main(int argc,char* argv[]){ if(argc != 3){ printf("Usage:%s [ip] [port]\n",argv[0]); return 1; } int sock = socket (AF_INET, SOCK_STREAM, 0); if(sock < 0){ printf("socket error!\n"); return 2; } struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(atoi(argv[2])); server.sin_addr.s_addr = inet_addr(argv[1]); if(connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0){ printf("connect error!\n"); return 3; } char buf[MAX]; while(1){ printf("please Enter# "); fflush(stdout); ssize_t s = read(0,buf,sizeof(buf)-1); if(s > 0){ buf[s-1] = 0; if(strcmp("quit",buf) == 0){ printf("client quit!\n"); break; } write(sock,buf,strlen(buf)); s = read(sock,buf,sizeof(buf)-1); buf[s] = 0; printf("server Echo# %s\n",buf); } } close(sock); return 0; }
When receiving a request, the server first creates a child process, and then the child process creates a grandchild process, and the grandchild process completes the interaction with the client.
- Advantages and disadvantages of the multi-process version
advantage:
(1) Can handle multiple user requests;
(2) The code is relatively simple and the writing cycle is short;
(3) Due to the independence of the process, the stability of the multi-process server is strong.
shortcoming:
(1) The child process is created only when the connection arrives, and creating a child process takes time and affects performance;
(2) Each process of the multi-process server occupies resources, resulting in a limited number of customers that it can serve;
(3) As the number of multi-process servers increases, the pressure on the CPU will increase, and the time required for CPU scheduling will become longer, which will affect performance.