Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the network server model of Linux operating system

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the Linux operating system network server model has, the article is very detailed, has a certain reference value, interested friends must read it!

1. TCP Loop Server:

First, the TCP server accepts a connection request from one client, processes the connection request, disconnects after completing all the client requests, and then accepts the request from the next client. The algorithm for creating a TCP circular server is as follows:

The code is as follows:

Socket (…) ; / / create a TCP socket

Bind (…) ; / / Bond the recognized port number

Listen (…) ; / / listen to client connections

While (1) / / start receiving client connection in a loop

{

Accept (…) ; / / receive the connection from the current client

While (1)

{/ / process the request from the current client

Read (…)

Process (…)

Write (…)

}

Close (…) ; / / close the current client connection and prepare to receive the next client connection

}

The TCP loop server only processes requests from one client at a time. If one client occupies the server, the other client connection requests will not be answered in time. As a result, TCP servers rarely use the circular server model.

2. TCP concurrent server:

The idea of the concurrent server is that the request of each client is not directly processed by the server's main process, but that the server's main process creates a child process to handle it. The algorithm for creating a TCP concurrent server is as follows:

The code is as follows:

Socket (…) ; / / create a TCP socket

Bind (…) ; / / Bond the recognized port number

Listen (…) ; / / listen to client connections

While (1) / / start to cycle the reception of the client

{

Accept (…) / / receive a connection from a client

If (fork (…) = = 0) / / create a child process

{

While (1)

{/ / the child process handles a client's connection

Read (…)

Process (…)

Write (…)

}

Close (…) ; / / close client connections handled by child processes

Exit (…) ; / / terminate the child process

}

Close (…) / / the parent process closes the connection socket descriptor and is ready to receive the next client connection

}

TCP concurrent server can solve the situation where the TCP round robin server client monopolizes the server. But at the same time, it also brings a big problem, that is, in response to the request of the client, the server has to create a child process to deal with it, and creating a child process is a very resource-consuming operation.

3. UDP Loop Server:

The UDP server reads the client's Datagram request one at a time from the socket, processes the received UDP Datagram, and then returns the result to the client. The algorithm for creating a UDP circular server is as follows:

1 socket (…) ; / / create a Datagram type socket 2 bind (...) ; / / set the accepted short slogan 3 while (1) / / start receiving client connection 4 {/ / receive and process client UDP Datagram 5 recvfrom (…) 6 process (.) 7 sendto (.) ; / / prepare to receive Datagram from the next client 8}

Eliminate line number

Because UDP is non-connection-oriented, no client can monopolize the server. As long as the processing process is not an endless loop, the server can always handle requests from each client.

When the Datagram flow of the UDP round robin server is too large, the client's technical data may be reported to be lost due to the heavy processing task, but because the UDP protocol itself does not guarantee the 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 circular mode 4. UDP concurrent server applies the concept of concurrency to UDP to get a concurrent UDP server, which is handled by creating sub-processes just like the concurrent TCP server model.

The algorithm for creating a UDP concurrent server is as follows:

The code is as follows:

Socket (…) ; / / create a socket of Datagram type

Bind (…) ; / / Bond recognized short slogans

While (1) / / start receiving client connection

{/ / receive and process UDP datagrams from the client

Recvfrom (…)

If (fork (…) = = 0) / / create a child process

{

Rocess (…)

Sendto (…)

}

}

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

4. Multiplexing Istroke O concurrent server:

The creation of child processes will lead to a large consumption of system resources. In order to solve this problem, the concurrent server of multiplexing Icano model is adopted. The algorithm of using select function to create a concurrent server with multiplexing Istroke O model is as follows:

Initialization (socket,bind,listen)

The code is as follows:

While (1)

{

Set the listening read-write file descriptor (FD_*)

Call select

If the listening socket is ready, a new connection request is established

{

Establish a connection (accept)

Add to the listening file descriptor

}

Otherwise, the description is a connected descriptor

{

Perform operations (read or write)

}

Multiplex Iramp O can solve the problem of resource constraints. This model actually applies the UDP loop model to TCP. This also brings some problems, such as because the server processes the customer's request in turn, it may cause the friend's customer to wait for a long time.

These are all the contents of the article "what are the Linux operating system network server models?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report