Lucas Moskun
Lucas Moskun

Reputation: 181

What is "socket + 1" achieving when making a select call? unix/c++

I am implementing a udp listen server based on this https://linux.m2osw.com/c-implementation-udp-clientserver. I noticed when establishing a timeout receiver the author included "f_socket+1" when making the select call. I am wondering what exactly this is doing? Any explanation helpful, thank you!

excerpt of function from above link:

    FD_ZERO(&s);
    FD_SET(f_socket, &s);
    struct timeval timeout;
    timeout.tv_sec = max_wait_ms / 1000;
    timeout.tv_usec = (max_wait_ms % 1000) * 1000;
    int retval = select(f_socket + 1, &s, &s, &s, &timeout); 

Upvotes: 1

Views: 1239

Answers (1)

Brian Bi
Brian Bi

Reputation: 119174

See https://pubs.opengroup.org/onlinepubs/007908799/xsh/select.html

The nfds argument specifies the range of file descriptors to be tested. The select() function tests file descriptors in the range of 0 to nfds-1.

Thus, that argument should be set to 1 greater than the maximum file descriptor you want to monitor.

Upvotes: 5

Related Questions