Initial understanding of iterative servers and concurrent servers

  
 

The iterative server is primitive, its prototype can be described as:
while(1) { new_fd = server accept client connection (new_fd = accept(listenfd, XX, XX)) Logic processing on this new_fd to the client Sending a message to close new_fd } That is, the process is to handle the connection sent by each client one by one. For example, if a client sends a connection, it will always occupy it as long as it has not completed its task. The server's process closes the socket until the server finishes processing. Concurrent server is the most frequently used: 1 while(1) 2 { 3 new_fd = server accept client connection 4 if (is a child process) 5 { 6 First turn off listening fd //because the child process does not need to listen, it only Responsible for processing logical concurrent messages to the client 7 Processing logic to send messages 8 Close new_fd 9 Close process 10 } 11 Close new_fd 12 } So every time a client, the server clones itself to process the request, so the main process is always listening And will not be blocked, I want to talk about the focus is not here, the focus is the code line 12! 1. Don't think that fork will generate two new socket descriptors when a child process comes out. In fact, the child process and the parent process share linten_fd and new_fd. When the parent process closes new_fd (code line 12) it just puts The access count value of this new_fd is reduced by one. Since the access count value is also > 0 (because there is still a client's new_fd attached), it does not disconnect from the client. 2. If we don't write the 12th line, the first thing, because the assignable socket descriptor is limited, if it is not released after the allocation, it can not be recycled, that is, there is always a day when the descriptor is exhausted. . Second, after the task of connecting to the client is handed over to the child process, the parent process can continue to listen and accept the next connection, but if the parent process does not close its connection with the client, it means that the connection is always there! Equivalent to every customer, this parent process has even a client and the connection is constant, you should know the consequences.

Copyright © Windows knowledge All Rights Reserved