Linux multi-process CS Server simple implementation server side
- Multi-process Implementation multi-user connection, that is, each user a connection, here still with the server will receive the string to capitalize back to the client.
#include <stdio.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <ctype.h> #include <stdlib.h> #include <sys/wait.h> #define SERV_IP " 127.0.0.1 "#define Serv_port 8000void wait_child (int signo) {while (Waitpid (0, NULL, Wnohang) >0); return;} int main (int argc,char *argv[]) {pid_t pid;//process id int SFD, cfd;//receive connected SFD, and client communication cfd struct SOCKADDR_IN serv_a DDR, clie_addr;//create server and client structure body socklen_t clie_addr_len;//client Structure body length char Buf[bufsiz], CLIE_IP[BUFSIZ];//BUF store received data int N, i;//read the number of data n, loop factor I sfd = socket (af_inet, sock_stream, 0);//Create Socket Bzero (&serv_addr, sizeof (SERV_ADDR));// Clear 0 serv_addr.sin_family = af_inet;//Set the protocol family to IPv4 serv_addr.sin_addr.s_addr = htonl (Inaddr_any); Set the NIC to any valid local Nic//inet_pton (Af_inet, Serv_ip, &serv_addr.sin_addr.s_addr); Serv_addr.sin_port = htons (serv_port);//Set port bind (SFD, (struct sockaddr *) &serv_addr, sizeof (SERV_ADDR));//Set socket with SFD Association listen (SFD, 128);//sets the maximum number of outstanding accept. Start listening. while (1)//loop receive Client connection {Clie_addr_len = sizeof (CLIE_ADDR);//Initialize client structure Body length cfd = accept (sfd, struct sockaddr *) &clie_addr, &clie_addr_len);//Receive Client Connection printf ("Client ip:%s, port:%d\n", Inet_ntop (Af_inet, &clie_addr . sin_addr.s_addr, Clie_ip, sizeof (CLIE_IP)), Ntohs (Clie_addr.sin_port)); PID = fork ();//Create a new process if (pid< 0)//error {perror ("fork Error"); Exit (1); } else if (PID = = 0)//child process {close (SFD);//child process first close parent process file descriptor break; } else if (pid>0)//parent process {Close (CFD);//parent process first close child process file descriptor signal (SIGCHLD, wait_child); }} if (PID = = 0)//The following is the true business processing part of the child process {while (1)//loop processing client Business {n = Read (CfD, buf, Sizeo F (buf)); if (n = = 0)//If Read returns 0 indicating that the client has disconnected {close (CFD);//Close the client file descriptor return 0; } else if (n = =-1)//error {perror ("read error"); Exit (1); } else {write (Stdout_fileno, buf, N); for (i= 0; i< n;++i) {Buf[i] = ToUpper (Buf[i]); } write (CfD, buf, N); }}} return 0;}
Client Side
- Connect to the server side, send a string, print the string that is received (that is, the same function as the NC command).
#include <stdio.h>#include <string.h>#include <unistd.h>#include <netinet/in.h>#include <arpa/inet.h>#define MAXLINE 8192#define SERV_PORT 8000int main(int argc,char *argv[]){ struct sockaddr_in servaddr; char buf[MAXLINE]; int sockfd, n; sockfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr); servaddr.sin_port = htons(SERV_PORT); connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); while(fgets(buf, MAXLINE, stdin)!=NULL) { write(sockfd, buf, strlen(buf)); n =Read(sockfd, buf, MAXLINE); if(n ==0) { printf("the other side has been closed.\n"); break; } else write(STDOUT_FILENO, buf, n); } close(sockfd); return 0;}
Test results
- You can implement multiple clients to connect to the server at the same time, the server creates a child process per customer, and the process is automatically reclaimed by the parent process after the client disconnects.
There is a problem
- Without error handling, interference may occur when a multiuser connection is made.
Simple implementation of Linux multi-process CS Server