简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM