Linux operating system network server model sharing

Source: Internet
Author: User
Tags bind resource socket port number

The so-called concurrent server is at the same time can handle requests from multiple clients; A circular server is a request that the server can respond to only one client at a time. And for TCP and UDP sockets, the two servers are implemented in different ways.  
1, TCP cycle server:
first, the TCP server accepts a client's connection request, processes the connection request, disconnects after completing all of the client's requests, and then accepts the next client request. The algorithm for creating a TCP loop server is as follows:

Copy Code code as follows:


socket (...); Create a TCP socket


bind (...); The
of the state recognized port number

Listen (...); Listen to client connections <span class="Apple-converted-space"></span>


while (1)//Start loop receive Client connection


{  


Accept (...); /Receive the current client connection  


while (1)


{//processing requests from the current client <span class="Apple-converted-space"></span>


read (...);  


process (...);  


write (...);  


}  


Close (...); Closes the current client connection and prepares to receive the next client <span class="Apple-converted-space"></span> connection


}  


TCP Circular Server only processes one client at a time, and if one client takes up the server, other client connection requests are not responded to in a timely manner. As a result, TCP servers typically use a circular server model very rarely.  


2, TCP concurrent server:


The idea of a concurrent server is that each client's request is not handled directly by the server's main process, but rather that the server main process creates a subprocess to process it. The algorithm for creating a TCP concurrency server is <span class="Apple-converted-space"></span> as follows:





Copy Code code as follows:


socket (...); Create a TCP socket


bind (...); The
of the state recognized port number

Listen (...); /Listen to client connections <span class="Apple-converted-space"></span>


while (1)//Start loop receive client receive


{  


Accept (...); /Receive a client's connection


if (fork ...) ==0)//Create child processes


{  


while (1)


{//Subprocess processes a client's connection


read (...);  


process (...);  


write (...);  


}  


Close (...); Closes the client connection for child process processing <span class="Apple-converted-space"></span>


exit (...);//Terminate the subprocess


}  


Close (...); The parent process closes the connection socket descriptor, ready to receive the next client connection <span class="Apple-converted-space"></span>


}  


TCP Concurrent servers can resolve the TCP circular Server client exclusive server. But at the same time there is a small problem, that is, in response to client requests, the server to create a child process to handle, and create a child process is a very resource-consuming operation.  


3, UDP cycle server:
the
UDP server processes received UDP datagrams each time a datagram request is read from a socket, and then returns the results to the client. The algorithm for creating a UDP loop server is <span class="Apple-converted-space"></span> as follows:


1 socket (...); Create a datagram type of Socket 2 bind (...); State-established short slogan 3 while (1)///Start receiving client's connection 4 {//Receive and process client's UDP datagram 5 recvfrom (...). 6 process (...); 7 sendto (...);../ /ready to receive the next client datagram 8}


Elimination line number


because UDP is not connection-oriented, no client can monopolize the server. As long as the process is not a dead loop, the server is always able to handle requests for each client.  


UDP loop server can cause the loss of customer technical data when the datagram traffic is too heavy, but because the UDP protocol itself does not guarantee the datagram to arrive reliably, the UDP protocol is allowed to lose the datagram.  


in view of the above two points, the general UDP server in the Loop Mode 4, UDP concurrent server to the concept of concurrent application UDP to get the concurrent UDP server, and the concurrent TCP server model is to create a child process to deal with.  


The algorithm for creating a UDP concurrent server is <span class="Apple-converted-space"></span> as follows:





Copy Code code as follows:


socket (...); Create a datagram type of socket


bind (...); The
of the state-established short slogan

while (1)//Start receiving client connections


{///Receive and process client UDP datagram


recvfrom (...);  


if (fork ...) ==0)//Create child processes


{  


rocess (...);  


sendto (...);  


}  


}  


unless the server takes a long time to process client requests, people actually rarely use this UDP concurrency server model.  


4, multiplexing I/O concurrent server:
in order to solve this problem, a concurrent server with multiplexing I/O model is used to create a
process that will lead to heavy consumption of system resources. The algorithm for creating a multiplexed I/O model with a SELECT function is as follows: <span class="Apple-converted-space"></span>

Initialization of
(Socket,bind,listen);  





Copy Code code as follows:


while (1)


{  


sets the listener read-write file descriptor (fd_*);  


call Select;  


If you are listening to sockets ready, indicate a new connection request to establish


{  


Establish the connection (accept);  


is added to the listener file descriptor;  


}  


Otherwise the description is an already connected descriptor


{  


for operation (read or write);  


}  


multiplexing i/o resolves resource constraints, and this model actually uses the UDP loop model for TCP. This can also bring some problems, such as because the server processes the client's request in turn, it may cause the friend's customer to 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.