One ,tcp Overview of concurrent servers

A good server , Generally, they are concurrent servers ( It can respond to requests from multiple clients at the same time ). Concurrent server design techniques generally include : Multiprocess server , Multithreaded server ,I/O Reuse servers, etc .

Two , Multi process concurrent server

stay Linux
There are many applications of multi process in the environment , One of the most important is the network / Client server . The multi process server is used when the client has a request , The server uses a child process to process client requests . The parent process continues to wait for requests from other clients .
The advantage of this approach is that when the customer has a request , The server can deal with customers in time , Especially in the client server interaction system . For a TCP
The server , The client server connection may not be closed immediately , You may wait until the customer submits some data before closing , During this time, the server-side process will be blocked , So the operating system may schedule other client service processes ,
This greatly improves service performance compared to circular servers .

to develop

tcp Multi process concurrent server

TCP The idea of concurrent server is that each client's request is not directly processed by the server , Instead, the server creates a child process to handle it .

tcp Multi process concurrent server reference code :
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>
/************************************************************************ Function name :
void main(int argc, char *argv[]) Function function : Principal function , Create a TCP Echo Server Function parameters : nothing
Function return : nothing
************************************************************************/ int
main(int argc, char *argv[]) { unsigned short port = 8080; // Local port //1. establish tcp socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd < 0) {
perror("socket"); exit(-1); } // Configure local network information struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr)); // empty my_addr.sin_family = AF_INET; // IPv4
my_addr.sin_port = htons(port); // port my_addr.sin_addr.s_addr =
htonl(INADDR_ANY); // ip //2. binding int err_log = bind(sockfd, (struct
sockaddr*)&my_addr, sizeof(my_addr)); if( err_log != 0) { perror("binding");
close(sockfd); exit(-1); } //3. monitor , Socket becomes passive err_log = listen(sockfd, 10);
if(err_log != 0) { perror("listen"); close(sockfd); exit(-1); } while(1) // Main process
Loop waiting for client connection { char cli_ip[INET_ADDRSTRLEN] = {0}; struct sockaddr_in
client_addr; socklen_t cliaddr_len = sizeof(client_addr); // Take out the completed connection of the client int
connfd = accept(sockfd, (struct sockaddr*)&client_addr, &cliaddr_len);
if(connfd < 0) { perror("accept"); close(sockfd); exit(-1); } pid_t pid =
fork(); if(pid < 0){ perror("fork"); _exit(-1); }else if(0 == pid){ // Subprocess
Receive client information , Concurrent return to client /* Closing unnecessary sockets saves system resources , At the same time, it can avoid the parent-child process from sharing these sockets The possible unpredictable consequences */
close(sockfd); // Close listening socket , This socket is inherited from the parent process char recv_buf[1024] = {0}; int
recv_len = 0; // Print client's ip And port memset(cli_ip, 0, sizeof(cli_ip)); // empty
inet_ntop(AF_INET, &client_addr.sin_addr, cli_ip, INET_ADDRSTRLEN);
printf("----------------------------------------------\n"); printf("client
ip=%s,port=%d\n", cli_ip,ntohs(client_addr.sin_port)); // receive data while( (recv_len
= recv(connfd, recv_buf, sizeof(recv_buf), 0)) > 0 ) { printf("recv_buf: %s\n",
recv_buf); // print data send(connfd, recv_buf, recv_len, 0); // Send data back to client }
printf("client_port %d closed!\n", ntohs(client_addr.sin_port)); close(connfd);
// Close connected socket exit(0); } else if(pid > 0){ // Parent process close(connfd); // Close connected socket } }
close(sockfd); return 0; }

Operation results :

Technology
©2019-2020 Toolsou All rights reserved,
【JAVA】【 Huawei campus recruitment written examination - Software 】2020-09-09( Essence )2020 year 7 month 21 day ASP.NET Core Use of global filters Golang Array bisection , Array split , Array grouping ( Essence )2020 year 8 month 2 day TypeScript The use of generics keras Data generator -- Data enhancement ToastUtils Use of ( Essence )2020 year 6 month 26 day C# Class library Exception handling help class element-ui Of el-date-picker Component get value Paging tool class PageResult Digital rolling lottery program