I think that NFDS in select() determines how many sockets the function will check in READFDS and the other fd_sets. So if we set 3 sockets in our fd_set, but I want to check only first one, I have to call select(1 + 1,…). Is this right?
Or does “nfds is the highest-numbered file descriptor in any of the three sets, plus 1” in linux select man means something different? Also why do we need to add + 1?
Example code – fixed
int CLIENTS[max_clients];//Clients sockets
int to_read;
FD_ZERO(&to_read);
int i;
int max_socket_fd = 0;
for (i = 0 ; i < max_clients ; i++)
{
if(CLIENTS[i] < 0)
continue;
int client_socket = CLIENTS[i];
if(client_socket > max_socket_fd)
max_socket_fd = client_socket;
FD_SET(client_socket , &to_read);
}
struct timeval wait;
wait.tv_sec = 0;
wait.tv_usec = 1000;
int select_ret = select(max_socket_fd + 1, &read_flags, NULL, NULL, &wait);
...
“nfds is the highest-numbered file descriptor in any of the three sets, plus 1“
Every file descriptor is represented by an integral value. So they are not asking for the
x-thdescriptor that you want to check, they are asking for the highest integral value of the descriptors in yourREADFDS+1.Btw, you should check out
poll(2)andppoll(2).