Skip to content
Advertisement

Meaning of adding 1 on socket file descriptor in select function when using linux socket

What is the meaning of adding 1 on socket file descriptor when using in select function?

I create socket file descriptor like below,

int sock_file_descriptor;
sock_file_descriptor = socket(AF_INET, SOCK_DGRAM, 0);

and use it in select function like below,

result = select(sock_file_descriptor+1, &readfd, NULL, NULL, 0);

What is the meaning of +1 in select function? It even does not work when I remove the calculation adding the value.

Thanks in advance.

Advertisement

Answer

RTFM! The first parameter of select is the number of file descriptors to considere:

The nfds argument specifies the range of descriptors to be tested. The first nfds descriptors shall be checked in each set; that is, the descriptors from zero through nfds-1 in the descriptor sets shall be examined.

Here is an example of use:

// create socket
int sock_file_descriptor;
sock_file_descriptor = socket(AF_INET, SOCK_DGRAM, 0);

// initialize the read fd_set
fd_set read;
FD_ZERO(&read);
FD_SET(sock_file_descriptor, &read);

// ok we can select
result = select(sock_file_descriptor+1, &readfd, NULL, NULL, 0);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement