套接字选择减少文件描述符集中的套接字数量
我有一段代码接受 2 个连接,创建一个文件描述符集及其各自的套接字,并将其传递给 select。但当select返回时,文件描述符集中的文件描述符数量减少为1,select只能检测fd_array数组中第一个socket接收到的数据。
我应该看看哪里有什么想法吗?
预先感谢,
安德烈
fd_set mSockets; /* At this point mSockets.fd_count = 2 mSockets.fd_array[0] = 3765 mSockets.fd_array[1] = 2436 */ select(0, & mSockets, 0, 0, 0); /* At this point mSockets.fd_count = 1 mSockets.fd_array[0] = 3765 mSockets.fd_array[1] = 2436 */
I have a piece of code that accepts 2 connections, creates a file descriptor set with their respective sockets, and passes it to select. But when select returns, the number of file descriptors in the file descriptor set was reduced to 1, and select can just detect received data for the first socket in the fd_array array.
Any ideas where I should look at?
Thanks in advance,
Andre
fd_set mSockets; /* At this point mSockets.fd_count = 2 mSockets.fd_array[0] = 3765 mSockets.fd_array[1] = 2436 */ select(0, & mSockets, 0, 0, 0); /* At this point mSockets.fd_count = 1 mSockets.fd_array[0] = 3765 mSockets.fd_array[1] = 2436 */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也就是说,select 函数的 readfds、writefds 和 exceptfds 参数被设计为输入/输出参数。
您应该在每次调用 select 之前初始化 fd_set:
此外,cou 可以检查 select 的返回值。如果您可以对套接字进行操作,则表示无效。即零返回表示所有 FD_ISSET amcros 将返回 0。
编辑:
由于 readfds、writefds 和 exceptfds 也是 select 函数的out 参数,因此它们被修改。 fd_count 成员指示有多少个 fd_array 成员有效。如果
fd_count
小于 2,则不应计算fd_array[1]
。That is by design the readfds, writefds and exceptfds paramters of the select functions are in/out paramters.
You should initialize the fd_set before each call to select:
Additionally cou can check the return value of select. It indicates invalid if you can opertate with the sockets at all. I.e. a zero return indicates, that all FD_ISSET amcros will return 0.
EDIT:
Since readfds, writefds and exceptfds are also out paramters of the select functions, they are modified. The fd_count member indicates how many fd_array members are valid. You should not evaluate
fd_array[1]
iffd_count
is less than 2.