1. Introduction
Multiplexing I/O model (select) is the most commonly used I/O model in UNIX/LINUX. It can also be used as a synchronous I/O model in Windows. This article provides a simple implementation of the I/O model to process multiple clients (in the main thread.
2. About select
The select I/O model is an asynchronous I/O model. By default, Linux/WinNT supports 64 client sockets in a single thread. This I/O model mainly involves the following functions and macros:
Int select (...) , FD_ZERO, FD_SET, FD_ISSET, and FD_SETSIZE.
3. Use select to develop a Server
3.1 supports only one Client
// Initialize and create a listening socket
Listen (listensock, 5 );
Clientsock = accept (listensock, NULL, NULL );
For (;;)
{
FD_ZERO (& readfds );
Fd_set (clientsock, & readfds );
Nresult = select (
0, // this parameter is ignored in windows. In Linux, this parameter is 1
Readfds, // a set of readable sockets
......
)
If (nResult = SOCKET_ERROR)
Return-1;
// Judge whether cliensock is in the waiting status
If (FD_ISSET (clientsock, & readfds ))
{
// Related processing
}
}
In fact, the WSAEventSelect model in Winsock is similar.
3.2 support 63 clients in a single thread
SOCKET clientsockarray [FD_SETSIZE-1]; // FD_SETSIZE is 64
// Initialize and create a listening socket
Listen (listensock, 5 );
// Initialize the socket Array
InitSock (clientsockarray );
FD_ZERO (& readfds );
FD_SET (listensock, & readfds );
For (;;)
{
NRet = select (0, & readfds, NULL );
// Determine whether the listening socket is readable
If (FD_ISSET (listensock, & readfds ))
{
Clientsock = accept (listensock, NULL, NULL );
// Put the customer socket into the socket Array
If (! InsertSock (clientsockarray, clientsock ))
{
Printf ("the number of clients exceeds 63, and the connection is rejected./n ");
Closesocket (clientsock );
Continue;
}
}
// Process sockets in a pending state one by one
For (nIndex = 0; nIndex <FD_SETSIZE-1; nIndex ++)
{
If (FD_ISSET (clientsockarray [nIndex], & readfds ))
{
NRet = recv (clientsockarray [nIndex], buff, sizeof (buff), 0 );
If (nRet = 0 | nRet = SOCKET_ERROR)
{
Closesocket (clientsockarray [nIndex]);
Clientsockarray [nIndex] = INVALID_SOCKET;
Continue; // continue to process other sockets in the socket handle Array
}
// Process the received data. Only the received data is output here.
Printf ("% s/n", buff );
}
}
// Initialize the socket set
FD_ZERO (& readfds );
FD_SET (listensock, & readfds );
// Add all valid socket handles to the socket handle Array
For (nIndex = 0; nIndex <FD_SETSIZE-1; nIndex ++)
{
If (clientsockarray [nIndex]! = INVALID_SOCKET)
FD_SET (clientsockarray [nIndex], & readfds );
}
}
BOOL InsertSock (SOCKET * pSock, SOCKET sock)
{
For (int nIndex = 0; nIndex <FD_SETSIZE-1; nIndex ++)
{
If (pSock [nIndex] = INVALID_SOCKET)
{
PSock [nIndex] = sock;
Break;
}
}
If (nIndex = FD_SETSIZE-1)
Return FALSE;
Return TRUE;
}
The above is just a brief code, and some helper functions are not provided. It is convenient to use select to support multiple clients. 63 clients can be supported in one thread. Multiple Threads can be used to support a larger number of clients.
4. Efficiency
4.1 efficiency of socket array Scanning
In the above program, there are multiple scans for the socket handle, which will definitely affect the efficiency. I don't know how my friends handle this problem.
4.2 real-time response to client Problems
The preceding program processes the socket to be determined one by one. If the response time of a client reaches a certain level, the response to other clients will certainly be affected. My solution is to generate a sub-thread to process the socket when it is in a readable pending state ------ receive and process data. In this way, the main thread continues its work and other clients can respond in a timely manner. Of course, when there are a large number of client requests, thread control will become a new problem.
If you create a server that supports a large number of clients in Unix/Linux, I chose the select I/O model first, this is because I still don't know what better I/O models Linux has. For WinNT, there are also completionport and overlapped, especially for transmission of large data volumes and only a small number of clients, overlapped can play a considerable role. Dear friends, please provide a good method to use select.