c - 为什么调用 select 时套接字文件描述符参数会加一?

标签 c sockets

每当我在 C 中看到 select 调用时,我都会看到它写为:

select(sock_fd + 1, &fdset, NULL, NULL, &tv)

或者类似的东西。增加文件描述符背后的含义是什么?

最佳答案

来自POSIX specification of select :

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.

也就是说,您给出集合的大小,其中每个描述符都是一个索引

描述符集基本上实现为数组,sock_fd(在您的情况下)是该数组的索引,而sock_fd + 1是要检查的数组的大小

关于c - 为什么调用 select 时套接字文件描述符参数会加一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050819/

相关文章:

c - 哪些 seqlock 实现是正确的?

c - 如何使用 C 程序从 xml 文件中删除节点?

c++ - 通过COM端口与ModEEG通信

sockets - 使用 J2ME 进行套接字编程

c - 为什么这个套接字/文件描述符分配无效?

从c中的数组语法计算地址

C/海湾合作委员会: dlopen() without needing dlsym()

python - 'socket.error : [Errno 9] Bad file descriptor' as I run my client

java - 使用 Java 中的套接字编程将数据流从客户端程序(在虚拟机中运行)发送到服务器程序(在主机操作系统上)

线程中的 Java 套接字 - 无数据传输