Linux operating system network server model

Source: Internet
Author: User
Tags bind 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:

The code is as follows:

Socket (...); Create a TCP socket

Bind (...); State-Established port number

Listen (...); Listening to client connections

while (1)//Start loop receive client connection

{

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

while (1)

{//processing requests from the current client

Read (...);

Process (...);

Write (...);

}

Close (...); Closes the current client connection and prepares to receive the next client connection

}

The TCP cycle server processes only one client's request at a time, and if one client occupies 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 concurrent server is as follows:

The code is as follows:

Socket (...); Create a TCP socket

Bind (...); State-Established port number

Listen (...); /Listen to client connections

while (1)//Start loop receive client reception

{

Accept (...); /Receive a connection from a client

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

{

while (1)

{//Subprocess processes a connection to a client

Read (...);

Process (...);

Write (...);

}

Close (...); Turn off client connections processed by child processes

Exit (...);//terminate the child process

}

Close (...); The parent process closes the connection socket descriptor and prepares to receive the next client connection

}

A TCP concurrency server can resolve a 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:

Each time a UDP server reads a client's datagram request from a socket, processes the received UDP datagram, and returns the result to the client. The algorithm for creating a UDP loop server is 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 datagram 8} for the next client

Eliminate line numbers

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.

The UDP loop server may cause the loss of the 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 as follows:

The code is as follows:

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

Bind (...); A short slogan recognized by the state

while (1)//Start receiving client connections

{//Receive and process UDP datagrams for clients

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 child process that will lead to a large consumption of system resources. The algorithm for creating a multiplexed I/O model using the Select function is as follows:

Initialization (Socket,bind,listen);

The code is as follows:

while (1)

{

Set up the listener read and write file descriptor (fd_*);

Call Select;

If you are listening to sockets ready, indicate that a new connection request is established

{

Establish a connection (accept);

Add to the listener file descriptor;

}

Otherwise the description is a descriptor that has already been connected

{

To operate (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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.