Linux network server

zhaozj2021-02-17  92

There are two main types of Linux system network server models: concurrent servers and loop servers. The so-called concurrent server can handle a request from multiple clients at the same time; the loop server means that the server refers to a request to respond to a client at the same time. And for TCP and UDP sockets, the implementation of both servers has different features.

1, TCP cycle server: First, the TCP server accepts a client's connection request, processing the connection request, disconnects after all requests of this client, and then accept the next client request.

The algorithm for creating a TCP loop server is as follows:

Socket (...); // Create a TCP socket

Bind (...); // Bonding recognized port number

Listen (...); // Listen to the client connection

While (1) // Start loop receiving client connection

{

accept (...); / / Receive the connection of the current client

While (1)

{/ Request for the current client

Read (...);

Process (...);

Write (...);

}

Close (...); // Close the current client connection, ready to receive the next client connection

}

The TCP loop server only processes only one client's request. If a client occupies the server, the other client connection request has not responded in a timely response. Therefore, TCP servers generally use a loop server model.

2, TCP concurrent server: The idea of ​​concurrent servers is that each client's request is not directly processed by the server's main process, but the server main process creates a child process.

The algorithm for creating TCP concurrent servers is as follows:

Socket (...); // Create a TCP socket

Bind (...); // Bonding recognized port number

Listen (...); // Listen to the client connection

While (1) // Start the recirculation of the reception of the client

{

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

IF (fork (...) == 0) // Creating a child process

{

While (1)

{// child process handle a client connection

Read (...);

Process (...);

Write (...);

}

Close (...); // Turns the client connection of sub-process processing

EXIT (...); // Terminate the child process

}

Close (...); // Parent process closes the connection socket descriptor, ready to receive the next client connection

}

The TCP concurrent server can solve the situation of the TCP loop server client exclusive server. But it also brings a small problem that responds to client requests, the server wants to create a child process, and the creative sub-process is a very resource-consuming operation.

3, UDP loop server: UDP server reads a client's datagram request each time, handles 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 socket type of datagram

Bind (...); // Bonding recognized short slogan

While (1) // Start receiving the client connection

{// Receive and process UDP datagram

Recvfrom (...);

Process (...);

Sendto (...);

// Prepare the data report for receiving the next client

}

Because the UDP is non-connewed, no client can exclusively. As long as the processing is not a dead loop, the server can always be handled for each client.

The UDP loop server is too large if the data report traffic is too large, due to the weight of the customer technology, but because the UDP protocol itself does not guarantee the datagram to reliably arrive, the UDP protocol is especially lost. In view of the above two points, the general UDP server uses a loop mode

4, UDP concurrent server

Applying UDP to concurrently applying UDP to process the UDP server, and the concurrent TCP server model is to create a sub-process.

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

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

Bind (...); // Bonding recognized short slogan

While (1) // Start receiving the client connection

{// Receive and process UDP datagram

Recvfrom (...);

IF (fork (...) == 0) // Creating a child process

{

Process (...);

Sendto (...);

}

}

Unless the time used by the server is more than the time for processing the client's request, people actually use this UDP concurrent server model.

5, multiplexed I / O concurrent servers: The creation sub-process will bring a lot of consumption of system resources. In order to solve this problem, the concurrent server of multiplex I / O models is used. The algorithm of the concurrent server with the SELECT function creates multiplex I / O models is as follows:

Initialization (Socket, Bind, Listen);

While (1)

{

Set the monitor read and write file descriptor (FD_ *);

Calling SELECT;

If you are listening to the socket ready, a new connection request is established.

{

Establish a connection (ACCEPT);

Add to the listening file descriptor;

}

Otherwise, it is a descriptor that has been connected.

{

Operation (READ or WRITE);

}

}

Multiplexing I / O can resolve resource restrictions, which is actually used to use the UDP cycle model in TCP. This will also bring some questions. If the server sequentially handles the customer's request, customers who may lead to friends will wait for a long time.

转载请注明原文地址:https://www.9cbs.com/read-31315.html

New Post(0)