Linux Network Programming--tcp Concurrent Server (multi-process)

Source: Internet
Author: User

I. Overview of TCP concurrent Servers

A good server, typically a concurrent server (a request that can respond to multiple clients at the same time). Concurrent server design techniques generally include: multi-process servers, multi-threaded servers,I/O multiplexing servers, etc.


second, multi-process concurrent server

There are many applications in the Linux environment, the most important of which is the network/client server. A multi-process server is when a client has a request, the server uses a subprocess to process the customer request. The parent process continues to wait for requests from other customers. The advantage of this approach is that when the customer has a request, the server can process the customer in time, especially in the client server interaction system. For a TCP server, the client connection to the server may not be closed immediately, may wait until the customer submits some data and then shut down, this time the server side of the process will be blocked, so the operating system may dispatch other customer service processes, this is compared to the Loop server greatly improve service performance .

Developed

TCP Multi-process concurrent server

The idea of a TCP concurrent server is that each client's request is not processed directly by the server, but rather by the server creating a subprocess to process.


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: Main function, set up a TCP Echo server function parameter with process: No function return: no * * * /int Main (int argc, char *argv[]) {unsigned   Short port = 8080;//Local Port//1. Create a 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;  Ipv4my_addr.sin_port = htons (port); Port my_addr.sin_addr.s_addr = htonl (Inaddr_any); IP//2. bind int err_log = bind (SOCKFD, (struct sockaddr*) &my_addr, sizeof (MY_ADDR)), if (err_log! = 0) {perror ("binding Close (SOCKFD); exit (-1);} 3. Listen, socket becomes passive Err_log = Listen (SOCKFD, 10); if (Err_log! = 0) {PERror ("Listen"); Close (SOCKFD); exit (-1);} while (1)//main process loop waits for client's connection {char Cli_ip[inet_addrstrlen] = {0};struct sockaddr_in client_addr;socklen_t Cliaddr_len = sizeof (CLIENT_ADDR);//Remove client completed connection 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) {///Sub-process receives the client's information, and also returns the client////To close the unwanted sockets to save system resources, while avoiding the unpredictable consequences that parent-child processes may have on sharing these sockets */close (SOCKFD); Turn off the listener socket, which is inherited from the parent process char recv_buf[1024] = {0};int Recv_len = 0;//The IP and port memset of the print client (cli_ip, 0, sizeof (CLI_IP)); Empty Inet_ntop (Af_inet, &client_addr.sin_addr, Cli_ip, Inet_addrstrlen);p rintf ("------------------------------ ----------------\ n ");p rintf (" 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, re Cv_len, 0); Returning data to the client}printF ("Client_port%d closed!\n", Ntohs (Client_addr.sin_port)); close (CONNFD); Close connected sockets exit (0);}    else if (PID > 0) {//Parent process close (CONNFD);    Close connected sockets}}close (SOCKFD); return 0;}


Operation Result:

Linux Network Programming--tcp Concurrent Server (multi-process)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.