Server Design technology has many, according to the protocol used to divide the TCP server and UDP server. The loop server and the concurrent server are divided by processing mode.
1 cyclic server and concurrent server model
In the network program, in general, many customers correspond to a server, in order to deal with customer requests, the service side of the program has put forward special requirements.
Currently the most commonly used server models are:
• Loop server: The server can only respond to requests from one client at a time
• Concurrent servers: Servers can respond to requests from multiple clients at the same time
1.1 UDP How to implement a cyclic server:
UDP each time the loop server reads a client-side request from the socket, it then returns the result to the client.
Because UDP is non-connection-oriented, no client can always occupy the server. As long as the process is not a dead loop, the server is always satisfied with each client's request.
UDP the Loop server model is:
Socket (...);
Bind (...);
while (1)
{
recvfrom (...);
process (...);
sendto (...);
}
1.2 TCP How to implement a cyclic server
TCP The Loop server accepts a client connection and then processes, disconnects after all requests for this client have been completed. The TCP loop server can only process requests from one client at a time, and the server may continue with subsequent requests only after all requests from the client are satisfied. If there is a client that does not hold the server, the other clients do not work, so the TCP server is generally rarely used in the loop server model.
TCP the Loop server model is:
Socket (...);
Bind (...);
Listen (...);
while (1)
{
Accept (...);
process (...);
Close (...);
}
23 Methods of concurrent server implementation
A good server, usually a concurrent server. Concurrent server design techniques generally include: multi-process servers, multi-threaded servers, I/O multiplexing servers, etc.
2.1 Multi-process Concurrent server
There are many applications in the Linux environment, the most important of which is the network/client server. A multi-process server is when a client has a request, the server uses a subprocess to process the customer request. The parent process continues to wait for requests from other customers. The advantage of this approach is that when the customer has a request, the server can process the customer in time, especially in the client server interaction system. For a TCP server, the client's connection to the server may not be shut down immediately, it may wait until the customer submits some data and then shut down, and the server-side process is blocked, so the operating system may dispatch other customer service processes. Service performance is greatly improved compared to the loop server.
TCP Multi-process concurrent Server
TCP the idea of a concurrent server is that each client's request is not processed directly by the server, but rather by the server creating a subprocess to process.
Socket (...);
Bind (...);
Listen (...);
while (1)
{
accpet (...);
if (fork (...) = = 0)
{
process (...);
Close (...);
exit (...);
}
Close (...);
}
2.2 multi-threaded server
Multi-threaded server is an improvement to multi-process server, because the multi-process server consumes the large system resources when creating the process, so the service handlers can be created faster by replacing the process with the thread. According to statistics, creating a thread is 10,100 times times faster than creating a process, so the thread is called a "lightweight" process. Threads are different from processes: all threads within a process share the same global memory, global variables, and so on. This mechanism also brings about synchronization problems. The following is a multithreaded server template:
Socket (...);
Bind (...);
Listen (...);
while (1)
{
accpet (...);
if ((Pthread_create (...))! ==-1)
{
process (...);
Close (...);
exit (...);
}
Close (...);
}
2.3 I/O Multiplexing Server
I/O Multiplexing is a technique that occurs to resolve a process or thread blocking a call to an I/O system so that the process does not block a particular I/O system call. It can also be used for concurrent server design , commonly used functions select or poll to implement .
Socket (...);
Bind (...);
Listen (...);
while (1)
{
if (select (...) >0)
if (Fd_isset (...) >0)
{
Accpet (...);
process (...);
}
Close (...);
}
The above are TCP server-side programs, TCP client programs can be common:
Socket (...);
Connect (...);
Listen (...);
Process (...);
Close (...);
Three implementation models of concurrent servers in Linux environment