Do not know the network server model of the Linux operating system

Source: Internet
Author: User

There are two types of network server models in Linux: Concurrent Server and cyclic server.
 
A concurrent server can process requests from multiple clients at the same time. A cyclic server can respond to requests from a client at the same time. For TCP and UDP sockets, the two server implementation methods have different features.
 
1. TCP cyclic Server:
 
First, the TCP Server accepts the connection request from a client, processes the connection request, disconnects the client after completing all the requests, and then accepts the request from the next client.
 
The algorithm for creating a TCP cyclic server is as follows: socket (......); // Create a TCP socket bind (......); // Specify the accepted port number listen (......); // Listen to the client connection while (1) // start to receive the client connection cyclically {accept (......); // Receives the connection from the current client while (1)
 
{// Process the current client request read (......); Process (......); Write (......);} Close (......); // Close the connection of the current client and prepare to receive the next client connection} the TCP cyclic Server Processes requests from only one client at a time. If one client occupies the server, other client connection requests cannot be responded in a timely manner. Therefore, the cyclic server model is rarely used for TCP servers.
 
2. TCP Concurrent Server:
 
The idea of a concurrent server is that the request of each client is not directly handled by the master process of the server, but created by the master process of the server to process the request.
 
The algorithm for creating a TCP concurrent server is as follows: socket (......); // Create a TCP socket bind (......); // Specify the accepted port number listen (......); // Listen to the client connection while (1) // start to receive the client's receipt cyclically {accept (......); // Receives a client connection if (fork (......) = 0) // create a sub-process {while (1)
 
{// The sub-process processes the connection read (...) of a client (......); Process (......); Write (......);} Close (......); // Close the client connection for sub-process processing exit (......) ; // Terminate the sub-process} close (......); // The parent process closes the socket Descriptor and prepares to receive the next client connection} the TCP concurrent server can solve the problem where the TCP cyclic Server Client exclusively occupies the server. However, it also brings about a major problem: in response to client requests, the server needs to create sub-processes for processing, and creating sub-processes is a resource-consuming operation.
 
3. UDP circulating Server:
 
Each time the UDP server reads a client's datagram request from the socket, processes the received UDP datagram, and then returns the result to the client.
 
The algorithm for creating a UDP loop server is as follows: socket (......); // Create a datagram-type socket bind (......); // The well-recognized short slogan while (1) // starts to receive the client connection {// receives and processes the UDP datagram recvfrom (…) of the client (......); Process (......); Sendto (......); // Prepare to receive the datagram from the next client} because UDP is not connection-oriented, no client can exclusively occupy the server. As long as the processing process is not an endless loop, the server can always process requests from each client.
 
When the datagram traffic is too large, the UDP cyclic server may cause technical datagram loss due to heavy processing tasks. However, because the UDP protocol itself does not guarantee reliable arrival of the datagram, The UDP protocol allows the loss of the datagram.
 
In view of the above two points, the general UDP server adopts the loop mode 4. The UDP Concurrent Server applies the concept of concurrency to UDP and gets the concurrent UDP server, like the concurrent TCP server model, a sub-process is created for processing.
 
The algorithm for creating a UDP concurrent server is as follows: socket (......); // Create a datagram-type socket bind (......); // The well-recognized short slogan while (1) // starts to receive the client connection {// receives and processes the UDP datagram recvfrom (…) of the client (......); If (fork (......) = 0) // create a sub-process {process (......); Sendto (......);} Unless the server takes a long time to process client requests, this UDP concurrency server model is rarely used.
 
 4. multiplexing I/O concurrent servers:
 
Creating a sub-process will consume a lot of system resources. To solve this problem, a concurrent server that adopts the multiplexing I/O model is used. The algorithm for using the select function to create a multiplexing I/O model for concurrent servers is as follows:
 
Initialization (socket, bind, listen); while (1)
 
{Set listener read/write file descriptor (FD _ *); Call select; if the listener socket is ready, a new connection request is established {establish a connection (accept ); added to the listener file descriptor;} Otherwise, it indicates that it is a connected descriptor {perform operation (read or write);} multiplexing I/O can solve the Resource Restriction problem, this model actually uses the UDP loop model on TCP. This also causes some problems. For example, if the server processes client requests in sequence, the client may wait for a long time.

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.