简体   繁体   English

使用linux socket时select函数中socket文件描述符加1的意义

[英]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?在select函数中使用时socket文件描述符加1是什么意思?

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? select函数中+1是什么意思? It even does not work when I remove the calculation adding the value.当我删除添加值的计算时,它甚至不起作用。

Thanks in advance.提前致谢。

RTFM! RTFM! The first parameter of select is the number of file descriptors to considere: select的第一个参数是要考虑的文件描述符的数量:

The nfds argument specifies the range of descriptors to be tested. nfds 参数指定要测试的描述符的范围。 The first nfds descriptors shall be checked in each set;应在每个集合中检查第一个 nfds 描述符; that is, the descriptors from zero through nfds-1 in the descriptor sets shall be examined.即,应检查描述符集中从 0 到 nfds-1 的描述符。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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