Method for realizing blocking type multi-path IO (Input / Output) multiplexing by single thread
By using single thread to realize blocking multiple IO multiplexing in the server side, and using the select() function to monitor sockets, the problem of insufficient lightweight and large thread overhead in the existing technology is solved, and efficient and stable multi-client communication, suitable for resource-deficient embedded devices.
Patent Information
- Application Number
- CN202411460076.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-18
- Publication Date
- 2024-12-27
AI Technical Summary
The server architecture in the existing multiplexed IO multiplexed communication systems is not light enough, the thread overhead is large and the system resources are continuously occupied. Especially in small embedded devices, computing resources are scarce, making it difficult to meet the needs of multiple client connections.
Using single thread to implement blocking multiple IO multiplexing method, the listening socket and the customer communication socket are placed in the same read file descriptor to monitor, and the select() function is used to block and wait for new customers to connect and data transmission, avoiding Multithreaded scheduling and switching overhead, and a first-come-first-service response mechanism is implemented through connected customer linked lists.
It realizes lightweighting of the server architecture, reduces system resource usage, improves the efficiency and stability of the communication network, is suitable for single-core or multi-core CPUs, supports first-come-first service response of multiple clients, and detects broken links through heartbeat packets .
Smart Images

Figure CN119211383A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a method for implementing blocking multi-path IO multiplexing with a single thread. Background Art
[0002] In the fields of Internet, big data, aerospace, etc., the use of network communication is inevitable. Therefore, there is a need to use network sockets to realize multi-machine communication in various scenarios. In network communication, the client and server connection communication method under the C / S network architecture is very common. In the C / S network architecture, the server often establishes connections with multiple clients through network protocols and transmits data.
[0003] In the current common server-side architecture, the server needs to monitor client connection requests and data transmission requests in real time. Faced with the resource scheduling and management problem of multiple clients connecting at the same time, the simpler way to deal with it is for the server to allocate one or more threads to each client connection to achieve a quick response to client connections and data transmission. However, this processing method consumes a lot of configuration resources, and the equipment construction and operation costs are very high. The operating pressure of the server network is also very high, especially in small embedded devices, where computing resources are particularly scarce. Therefore, it is necessary to optimize the server-side architecture to meet a certain amount of client connection requirements with less thread overhead. Chinese patent publication number CN114237937A, publication date March 25, 2022, entitled "A multi-threaded data transmission method and device" discloses a data transmission method, which monitors each socket interface in real time, caches the received data to the message queue associated with the socket interface, and reads and parses the received data in the message queue in sequence through the message queue thread, and sends data packets to the master server or control client according to the received data type, thereby improving data processing efficiency. However, this method uses multiple threads to implement monitoring and response, with large resource overhead, and the threads therein adopt a non-blocking operation mode, which requires continuous occupation of system resources, and the server-side architecture is not lightweight enough. Summary of the invention
[0004] The present invention overcomes the problems of the existing multi-channel IO multiplexing communication system, such as the server-side architecture is not lightweight enough, the thread overhead is large and the system resources are continuously occupied, and the like, and provides a method for realizing blocking multi-channel IO multiplexing with a single thread. The method places the listening socket and the client communication socket in the same read file descriptor set and monitors them simultaneously, accepts new client connection requests in the listening queue when the listening socket is readable, and receives data transmitted by the client when the client communication socket is readable. Only a single thread is needed to realize the connection and communication between the server and multiple clients, so that the server-side architecture is more lightweight. When there is no client request, the idle thread will enter a blocking state to release most of the occupied system resources, and the single-threaded structure avoids the overhead of multi-threaded scheduling and switching, so that the communication network runs more efficiently and stably, and at the same time ensures that the server-side program can run on both single-core or multi-core CPUs. And by creating a linked list of connected clients and inserting the links of newly connected clients at the end of the linked list, querying the readability of each client's communication socket from the head node of the linked list, and responding to client communication needs in order, a first-come-first-served response mechanism for multiple clients is conveniently implemented; by enabling and setting parameters for the TCP built-in heartbeat packet of each communication socket, the communication disconnection detection and processing mechanism for blocked sockets is guaranteed.
[0005] In order to achieve the above object, the present invention adopts the following scheme: The method for implementing blocking multi-channel IO multiplexing with a single thread is characterized by comprising the following steps: S1: Create a listening socket ListenFd in the server thread for listening to client connection requests, create a connected client list for saving the client communication socket CltSock, create an initially empty read file descriptor set rdfdset of the fd_set type, and call CltSock = accept(ListenFd, ...) to block the listening and wait for the first new client connection; S2: When a new client connection request is monitored, a new link is created to save the client communication socket CltSock generated by the accept() function in response to the client connection request, and the new link is inserted into the tail of the connected client link list; S3: Add the listening socket ListenFd and the client communication socket CltSock in the connected client list to the read file descriptor set rdfdset in turn, call the select() function to monitor the read file descriptor set rdfdset and filter the readable sockets therein, if no readable sockets are monitored, block and wait at the select() function, otherwise make the following responses to the filtered readable sockets in turn: If the listening socket ListenFd is readable, the accept() function is called and step S2 is executed; if the client communication socket CltSock is readable, the client data received by the socket is read; After responding to all readable sockets, the read file descriptor set rdfdset is cleared and step S3 is iteratively executed.
[0006] Preferably, in step S3, the method for screening out readable sockets and responding in sequence is: calling the function select() to determine in sequence whether each socket in the read file descriptor set rdfdset is readable, removing the unreadable sockets, and changing the read file descriptor set rdfdset into a readable socket set, and then calling the function FD_ISSET() to determine in sequence whether the listening socket ListenFd and the client communication socket CltSock are in the readable socket set; if the listening socket ListenFd is in the readable socket set, respond to the client connection request, and make a data reception response to the client communication socket CltSock in the readable socket set.
[0007] Preferably, the process of calling the function FD_ISSET() to sequentially determine whether the listening socket ListenFd and the client communication socket CltSock are in the filtered readable socket set includes: Call FD_ISSET (ListenFd, &rdfdset) to determine whether the listening socket ListenFd is still in the file set rdfdset, and determine whether the listening queue is empty through the function return value. If the listening queue is not empty, call the accept() function to execute step S2; Call FD_ISSET (fd, &rdfdset) in sequence with each client communication socket CltSock stored in the connected client list as the incoming parameter fd, and determine whether the client communication socket CltSock is readable based on the return value. If it is readable, read the client communication socket CltSock and cache and use the received client data.
[0008] Preferably, in step S1, the server creates a listening socket ListenFd in the following manner: Call the socket() function to create a socket and call the setsockopt() function to set the SO_REUSEADDR socket option of the socket, then call the bind() function to bind the socket to the specific IP address assigned by the system, and then call the listen() function with this socket as the input parameter to create a listening queue to convert this socket to obtain the listening socket ListenFd.
[0009] Preferably, both the listening socket ListenFd and the client communication socket CltSock adopt blocking sockets, and when creating and using the blocking sockets, it is selected not to call the fcntl function to modify the socket properties thereof.
[0010] Preferably, in step S3, when the function select() is called to monitor the read file descriptor set rdfdset and filter the readable sockets therein, if it is determined that a client has disconnected, the client communication socket CltSock corresponding to the client is closed and the corresponding link is deleted from the connected client linked list.
[0011] Preferably, the method of determining that the client has been disconnected includes at least: Read the handshake instruction data of the agreed closing time from the client communication socket CltSock, the format of which is agreed in advance by the server and the client; When calling the recv, recvfrom or read function to read the client communication socket, the return value of the function is less than 0; The capture program of the SIG_PIPE signal is preset and the SIG_PIPE signal closed by the corresponding client is captured.
[0012] Preferably, in step S2, the process of creating a new link to save the client communication socket CltSock includes: When there is at least one client connection request in the listening queue of the listening socket ListenFd, the thread is awakened from the call of the function select() and continues to run. The function accept() reads the first client connection request in the listening queue, establishes a connection with the client, generates and returns a new client communication socket CltSock, creates a new link to save the new client communication socket CltSock and allocates a corresponding receive buffer for it, saves the first address of the receive buffer in the new link, and then inserts the new link to the end of the connected client linked list.
[0013] Preferably, the server thread runs in a Unix-like operating system, and uses & as a running parameter or calls the system in the server thread and passes in a shell instruction to make the program of the server thread run in the system background.
[0014] Preferably, after generating the client communication socket CltSock, if it is a TCP socket, the functions setsockopt(CltSock, SOL_SOCKET, SO_KEEPALIVE, …), setsockopt(CltSock, SOL_TCP, TCP_KEEPIDLE, …), setsockopt(CltSock, SOL_TCP, TCP_KEEPINTVL, …), and setsockopt(CltSock, SOL_TCP, TCP_KEEPCNT, …) are called respectively to enable TCP heartbeat detection and set related heartbeat parameters.
[0015] The present invention has at least the following beneficial effects: (1) multiplexing of multiple IOs can be completed using only one thread, thus eliminating the overhead of multi-thread scheduling and thread switching in the server-side architecture, and making the server-side system run more efficiently and stably; (2) the listening socket and all client communication sockets are added to the read file descriptor set to be monitored before calling the select() function. When the thread is idle, it blocks and waits to free up system resources, and can wake up the thread in time to respond to requests from new clients and connected clients; (3) based on a single-thread implementation, the server-side architecture designed by this method can run on a single-core processor, making the communication system lighter and saving cost; (4) options can be set to enable The server program constructed by this method is run in the system background to facilitate system management and maintenance operations; (5) By using a blocking socket, the SO_REUSEADDR socket option of the listening socket can be set to speed up the restart speed of the server when an error occurs; (6) By creating a linked list of connected customers and inserting the link of the newly connected customer at the end of the linked list, querying the readability of each customer communication socket from the head node of the linked list, and responding to customer communication needs in order, a first-come, first-served response mechanism for multiple customers is conveniently implemented; (7) The TCP built-in heartbeat packet of each communication socket is enabled and the parameters are set, which ensures the communication disconnection detection and processing mechanism for the blocking socket. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 A schematic diagram of the operating principle of a server-side architecture thread constructed by the method for implementing blocking multi-path IO multiplexing with a single thread provided by the present invention; Figure 2 A partial code example diagram of an implementation method of the method for implementing blocking multi-path IO multiplexing with a single thread provided by the present invention. DETAILED DESCRIPTION
[0017] The present invention is further described in detail below in conjunction with the accompanying drawings so that those skilled in the art can implement the invention with reference to the description.
[0018] like Figure 1 As shown, the method for implementing blocking multi-path IO multiplexing with a single thread provided by the present invention comprises the following steps: S1: Create a listening socket ListenFd in the server thread for listening to client connection requests, create a connected client list for storing the client communication socket CltSock, create an initially empty read file descriptor set rdfdset of type fd_set, call CltSock = accept(ListenFd, ...) to block the listening and wait for the first new client connection; the function passed parameter omitted in CltSock = accept(ListenFd, ...) is the size of the listening queue. The server architecture adopts a single thread to respond to the connection request of the new client and the data transmission request of the connected client at the same time. The unified response method is: all the client communication sockets CltSocks that establish the connection are placed in a connected client list, and after each response is completed, the listening socket ListenFd and the client communication socket CltSock in the connected client list are updated to the read file descriptor set rdfdset for unified monitoring. When any of the sockets is in a readable state, a system response to establish a new connection or transmit data will be made according to the type of the readable socket. Based on this operating principle, there is no need to set a listening thread for the new client connection to wait, nor is there a need to set a thread responsible for data transmission for the connected client. A single thread can meet both requirements at the same time. Step S1 is used to initialize the operating conditions of the server. The thread does not open or create any file before creating the listening socket ListenFd to ensure that the created listening socket ListenFd and the communication socket CltSock can be allocated a smaller file descriptor by the operating system to improve the efficiency of calling the select() function to monitor the read file description set rdfdset. Before the first new client connects, the connected client list is empty, and there is no need to use the read file descriptor set rdfdset. You only need to use the accept() function on the listening socket ListenFd to block the listening and wait for the first new client to connect.
[0019] S2: When a new client connection request is monitored, a new link is created to save the client communication socket CltSock generated by the accept() function in response to the client connection request, and the new link is inserted into the tail of the connected client list; when the accept() function responds to the client connection request, a corresponding new client communication socket CltSock is generated for the newly connected client, and the new link is used to save the client communication socket CltSock and insert it into the tail of the connected client list. In the linked list reading mode, the client communication socket CltSock saved in each link is read starting from the head node of the linked list, thereby ensuring that the data sent by the first connected client can be received and processed first, thus realizing a first-come, first-served priority service mechanism.
[0020] S3: Add the listening socket ListenFd and the client communication socket CltSock in the connected client list to the read file descriptor set rdfdset in turn, call the select() function to monitor the read file descriptor set rdfdset and filter the readable sockets therein, if no readable sockets are monitored, block and wait at the select() function, otherwise make the following responses to the filtered readable sockets in turn: If the listening socket ListenFd is readable, the accept() function is called and step S2 is executed; if the client communication socket CltSock is readable, the client data received by the socket is read; when there is no new client connection request, the listening queue of the listening socket ListenFd is empty, and the thread is blocked at the accept() function call until at least one client connection request is received in the listening queue, at which time the thread is awakened and continues to run.
[0021] After responding to all readable sockets, the read file descriptor set rdfdset is cleared and step S3 is iteratively executed.
[0022] The calling method of the function select() is select(MaxFd+1,&rdfdset, NULL, NULL, NULL), where MaxFd is the maximum value stored in the client communication socket CltSock in the connected client list. The read file descriptor set rdfdset re-adds the sockets to be monitored after each clearing. The first socket is the listening socket ListenFd, followed by the client communication socket CltSock read in sequence from the connected client list. Therefore, when receiving a connection request from a new client and a data transmission request from an already connected client at the same time, the system will respond to the connection request from the new client first, and then perform the corresponding data transmission operations in sequence according to the client connection order. When multiple new clients request to establish a connection at the same time, the connection request will be cached in the listening list corresponding to the listening socket ListenFd. The connection request initiated by the client calling the connect() function to the server is read in sequence by the accept() function and a connection is established for the new client. The generated client communication socket CltSock is used for full-duplex communication between the server and the connected client. When no new client connection request or data transmission request from an existing client is received, the sockets in the read file descriptor set rdfdset are unreadable. At this time, the thread will be blocked at the function select() that monitors the read file descriptor set rdfdset. Therefore, the thread can give up the system's computing resources when it is idle. When a new request appears, a readable socket appears in the read file descriptor set rdfdset, and the thread is awakened and continues to run from the function select(), so that it can make a timely response.
[0023] To further illustrate the detailed implementation of this method, Figure 2 The example code is a code example of the present method in practical application. Those skilled in the art can easily parse out the implementation principle and beneficial effects of the present method from the example code, and can get inspiration from it to perform operations such as code replacement, optimization or application transplantation.
[0024] This method can complete multi-channel IO multiplexing using only one thread, eliminating the overhead of multi-thread scheduling and thread switching in the server-side architecture, and making the server-side system run more efficiently and stably; after adding the listening socket and all client communication sockets to the read file descriptor set rdfdset to be monitored, the select() function is called, and the thread is blocked and waits for system resources to be released when idle, and can be awakened in time to respond to client requests; the response to client requests in the server constructed by this method adopts a single-threaded implementation method, so that the client connection and data processing response program are not only suitable for device configurations with multi-core processors, but can also run on single-core processors, avoiding the problem that single-core processors cannot handle multi-threaded tasks, making the communication system lighter and more applicable; by creating a linked list of connected clients and inserting the links of newly connected clients at the end of the linked list, querying the readability of each client communication socket from the head node of the linked list, and responding to client communication needs in order, a first-come, first-served response mechanism for multiple clients is conveniently implemented. In common server-side architectures, switching, scheduling, management, and resource allocation of multiple processes or threads all involve considerable time and memory overhead, because each processor core can only run one thread at a time. Even if the number of processor cores on the server reaches a level that can run the system architecture, when the number of processor cores is much smaller than the number of threads or processes created, the scheduling overhead of switching threads or processes will be more serious, causing the response speed and system operation efficiency of the multi-threaded or multi-process server-side architecture to be much slower than the server-side architecture constructed using a single-threaded multi-channel IO multiplexing method in this application.
[0025] In another technical solution, in step S3, the method of screening out readable sockets and responding in sequence is: calling the function select() to determine in sequence whether each socket in the read file descriptor set rdfdset is readable, removing the unreadable sockets, and changing the read file descriptor set rdfdset into a readable socket set, and then calling the function FD_ISSET() to determine in sequence whether the listening socket ListenFd and the client communication socket CltSock are in the screened out readable socket set; if the listening socket ListenFd is in the readable socket set, respond to the client connection request, and make a data reception response to the client communication socket CltSock in the readable socket set.
[0026] The process of calling the function FD_ISSET() to determine whether the listening socket ListenFd and the client communication socket CltSock are in the filtered readable socket set includes: Call FD_ISSET (ListenFd, &rdfdset) to determine whether the listening socket ListenFd is still in the file set rdfdset, and determine whether the listening queue is empty through the function return value. If the listening queue is not empty, call the accept() function to execute step S2; Call FD_ISSET (fd, &rdfdset) in sequence with each client communication socket CltSock stored in the connected client list as the incoming parameter fd, and determine whether the client communication socket CltSock is readable based on the return value. If it is readable, read the client communication socket CltSock and cache and use the received client data.
[0027] In another technical solution, in step S1, the server creates a listening socket ListenFd in the following manner: Call the socket() function to create a socket and call the setsockopt() function to set the SO_REUSEADDR socket option of the socket, then call the bind() function to bind the socket to the specific IP address assigned by the system, and then use this socket as an input parameter to call the listen() function to create a listening queue to convert this socket to the listening socket ListenFd. This setting method ensures that the newly created listening socket ListenFd can be used immediately when the server program with an error is restarted.
[0028] In another technical solution, both the listening socket ListenFd and the client communication socket CltSock use blocking sockets, and when creating and using the blocking sockets, it is selected not to call the fcntl function to modify its socket properties. The currently common multi-channel IO multiplexing architecture often uses a non-blocking method to query whether each socket has readable data when monitoring whether readable data is received in each client socket, that is, calling the function fcnctl to change the socket attribute to a non-blocking socket and passing in a timeout when calling the select() function. When the timeout is within and the select() return value is greater than 0, each socket is used as an input parameter socket to call the function FD_ISSET (socket, &fdset) in turn and judge whether the file is readable according to whether the return value is 1. This periodic query greatly wastes the CPU's occupied time, and the method provided by the present application can avoid this problem by using a blocking socket due to the operating mechanism.
[0029] In step S2, the process of creating a new link to save the client communication socket CltSock includes: When there is at least one client connection request in the listening queue of the listening socket ListenFd, the thread is awakened from the call of the function select() and continues to run. The function accept() reads the first client connection request in the listening queue, establishes a connection with the client, generates and returns a new client communication socket CltSock, creates a new link to save the new client communication socket CltSock and allocates a corresponding receive buffer for it, saves the first address of the receive buffer in the new link, and then inserts the new link to the end of the connected client linked list.
[0030] In another technical solution, in step S3, when the select() function is called to monitor the read file descriptor set rdfdset and filter the readable sockets therein, if it is determined that a client has disconnected, the client communication socket CltSock corresponding to the client is closed and the corresponding link is deleted from the connected client linked list.
[0031] The methods for determining that a client has been disconnected include at least: Read the handshake instruction data of the agreed closing time from the client communication socket CltSock, the format of which is agreed in advance by the server and the client; When calling the recv, recvfrom or read function to read the client communication socket, the return value of the function is less than 0; The capture program of the SIG_PIPE signal is preset and the SIG_PIPE signal closed by the corresponding client is captured.
[0032] In another technical solution, the server thread runs in a Unix-like operating system, and uses & as a running parameter or calls the system in the server thread and passes in a shell command to make the server thread program run in the system background. The server program runs in the system background, does not interfere with the user's use of the shell, and is convenient for system management and maintenance operations.
[0033] In another technical solution, after the client communication socket CltSock is generated, if it is a TCP socket, the functions setsockopt(CltSock, SOL_SOCKET, SO_KEEPALIVE, ...), setsockopt(CltSock, SOL_TCP, TCP_KEEPIDLE, ...), setsockopt(CltSock, SOL_TCP, TCP_KEEPINTVL, ...), setsockopt(CltSock, SOL_TCP, TCP_KEEPCNT, ...) are respectively called to enable TCP heartbeat detection and set relevant heartbeat parameters. By enabling and configuring TCP heartbeat detection, it is possible to detect and avoid the problem that the client corresponding to the client communication socket CltSock does not actively call close to close the client socket and occupies the communication service port for a long time when the network cable is unplugged, the computer is unexpectedly shut down, the process is stuck, etc.
[0034] It should be noted that, although the above describes the various steps in a specific order, it does not mean that the various steps must be performed in the above specific order. In fact, some of these steps can be performed concurrently or even in a different order, as long as the required functions can be achieved. The number of devices and processing scales described here are used to simplify the description of the present invention, and the application, modification and variation of the present invention are obvious to those skilled in the art.
[0035] Although the embodiments of the present invention have been disclosed as above, they are not limited to the applications listed in the specification and the implementation modes, and they can be fully applied to various fields suitable for the present invention. For those familiar with the art, additional modifications can be easily implemented. Therefore, without departing from the general concept defined by the claims and the scope of equivalents, the present invention is not limited to the specific details and the illustrations shown and described herein.
Claims
1. A method for implementing blocking multiple IO multiplexing with a single thread, characterized in that: The following steps are involved: S1: Create a listening socket ListenFd in the server thread for listening to client connection requests, create a connected client list for saving the client communication socket CltSock, create an initially empty read file descriptor set rdfdset of the fd_set type, and call CltSock = accept(ListenFd, ...) to block the listening and wait for the first new client connection; S2: When a new client connection request is monitored, a new link is created to save the client communication socket CltSock generated by the accept() function in response to the client connection request, and the new link is inserted into the tail of the connected client link list; S3: Add the listening socket ListenFd and the client communication socket CltSock in the connected client list to the read file descriptor set rdfdset in turn, call the select() function to monitor the read file descriptor set rdfdset and filter the readable sockets therein, if no readable sockets are monitored, block and wait at the select() function, otherwise make the following responses to the filtered readable sockets in turn: If the listening socket ListenFd is readable, the accept() function is called and step S2 is executed; if the client communication socket CltSock is readable, the client data received by the socket is read; After responding to all readable sockets, the read file descriptor set rdfdset is cleared and step S3 is iteratively executed.
2. The method for implementing blocking multiplexing of IO by a single thread according to claim 1, characterized in that: In the step S3, the method of screening out readable sockets and responding in sequence is: calling the function select() to determine in sequence whether each socket in the read file descriptor set rdfdset is readable, removing the unreadable sockets, and changing the read file descriptor set rdfdset into a readable socket set, and then calling the function FD_ISSET() to determine in sequence whether the listening socket ListenFd and the client communication socket CltSock are in the readable socket set, if the listening socket ListenFd is in the readable socket set, respond to the client connection request, and make a data reception response to the client communication socket CltSock in the readable socket set.
3. The method for implementing blocking multiple IO multiplexing with a single thread according to claim 2, characterized in that: The process of calling the function FD_ISSET() to determine whether the listening socket ListenFd and the client communication socket CltSock are in the filtered readable socket set includes: Call FD_ISSET (ListenFd, &rdfdset) to determine whether the listening socket ListenFd is still in the file set rdfdset, and determine whether the listening queue is empty through the function return value. If the listening queue is not empty, call the accept() function to execute step S2; Call FD_ISSET (fd, &rdfdset) in sequence with each client communication socket CltSock stored in the connected client list as the incoming parameter fd, and determine whether the client communication socket CltSock is readable based on the return value. If it is readable, read the client communication socket CltSock and cache and use the received client data.
4. The method for implementing blocking multiple IO multiplexing with a single thread according to claim 1, characterized in that: In step S1, the server creates a listening socket ListenFd in the following manner: Call the socket() function to create a socket and call the setsockopt() function to set the SO_REUSEADDR socket option of the socket, then call the bind() function to bind the socket to the specific IP address assigned by the system, and then call the listen() function with this socket as the input parameter to create a listening queue to convert this socket to obtain the listening socket ListenFd.
5. The method for implementing blocking multiplexing of IO by a single thread according to claim 1, characterized in that: The listening socket ListenFd and the client communication socket CltSock both adopt blocking sockets. When creating and using the blocking sockets, it is selected not to call the fcntl function to modify the socket properties thereof.
6. The method for implementing blocking multiple IO multiplexing with a single thread according to claim 1, characterized in that: In step S3, when the function select() is called to monitor the read file descriptor set rdfdset and screen the readable sockets therein, if it is determined that a client has been disconnected, the client communication socket CltSock corresponding to the client is closed and the corresponding link is deleted from the connected client linked list.
7. The method for implementing blocking multiple IO multiplexing with a single thread according to claim 6, characterized in that: The methods for determining that a client has been disconnected include at least: Read the handshake instruction data of the agreed closing time from the client communication socket CltSock, the format of which is agreed in advance by the server and the client; When calling the recv, recvfrom or read function to read the client communication socket, the return value of the function is less than 0; The capture program of the SIG_PIPE signal is preset and the SIG_PIPE signal closed by the corresponding client is captured.
8. The method for implementing blocking multiplexing of IO by a single thread according to claim 1, characterized in that: In step S2, the process of creating a new link to save the client communication socket CltSock includes: When there is at least one client connection request in the listening queue of the listening socket ListenFd, the thread is awakened from the call of the function select() and continues to run. The function accept() reads the first client connection request in the listening queue, establishes a connection with the client, generates and returns a new client communication socket CltSock, creates a new link to save the new client communication socket CltSock and allocates a corresponding receive buffer for it, saves the first address of the receive buffer in the new link, and then inserts the new link to the end of the connected client linked list.
9. The method for implementing blocking multiple IO multiplexing with a single thread according to claim 1, characterized in that: The server thread runs in a Unix-like operating system, and uses & as a running parameter or calls the system in the server thread and passes in a shell command to make the program of the server thread run in the system background.
10. The method for implementing blocking multiplexing of IO by a single thread according to claim 1, characterized in that: After the client communication socket CltSock is generated, if it is a TCP socket, the functions setsockopt(CltSock, SOL_SOCKET, SO_KEEPALIVE, …), setsockopt(CltSock, SOL_TCP, TCP_KEEPIDLE, …), setsockopt(CltSock, SOL_TCP, TCP_KEEPINTVL, …), and setsockopt(CltSock, SOL_TCP, TCP_KEEPCNT, …) are called respectively to enable TCP heartbeat detection and set related heartbeat parameters.
Citation Information
Patent Citations
Heartbeat packet asynchronous control method and apparatus
CN106254179A
Network communication system and method based on TCP / IP communication protocol
CN106375324A
System and method for counting number of online client sides based on TCP / IP communication protocol
CN106506253A
Service calling method and device applied to service system and computer equipment
CN118368317A
Method, apparatus and computer program product for processing data.
US20170346933A1
