带有空 fd 集的套接字选择

发布于 2024-08-28 02:11:16 字数 182 浏览 8 评论 0原文

假设您有一个 fd 集,其中可以有零个或多个套接字。当我尝试对空 fd 集调用 select 操作时,我得到的是 -1 作为已设置的 fd 的数量,这意味着错误。那么您建议如何克服这个问题,您可能会说如果为空则不要调用,但我有一个循环,并且任何时候 fd set 都可以容纳 0 个或更多套接字。解决这个问题的最佳方法是什么? (我们使用的是C语言)

Suppose you have an fd set which can have zero or more sockets in it. When I try to call select operation on empty fd set, what I get is -1 as the number of fds which are set, meaning error. So what would you suggest to overcome this problem, you might say do not call if empty but I have a loop and any time fd set can hold 0 or more sockets. What is the best approach about this problem? (we are on C programming language)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终弃我 2024-09-04 02:11:16

那是不对的。您应该能够指定 0 作为集合中文件描述符的数量。事实上,这就是我记得早期做超时代码的方式,使用没有描述符的 select 并适当设置了超时值。

如果返回 -1,您需要查看 errno 来了解问题所在。


啊,根据你的评论,WSAEINVAL 表示 WinSock。这解释了你的问题。这是一头可怕的野兽,应该让我们摆脱痛苦:-)

此页指出,如果超时值无效或所有三个描述符参数都为空,您将得到 WSAEINVAL。它后来指出FD_ZERO“将集合初始化为空集合”。我不知道最后一个片段是否意味着该参数仍被视为 NULL。假设你的超时没问题(因为如果你在其中一组中出现 FD,那么它就有效),情况可能就是这样。

我确实知道 Berkeley 套接字区分 NULL FD 集(即 NULL 指针)和空 FD 集(没有 FD 集的有效指针) - WinSock 可能不是那么通用,特别是因为 nfds 被忽略并包含只是为了与 Berkeley 兼容。

我怀疑(尽管我不确定)WinSock 可能希望您不要做您想做的事情。它很可能只是假设您做错了什么,并且应该仅对一个或多个 FD 集中的一个或多个套接字使用 select (因为 select 的唯一用途是没有 FD 是一种延迟,Windows 有很多更好的方法来实现延迟)。

That's not right. You should be able to specify 0 as the number of file descriptors in the set. In fact, that's how I remember doing timeout code in the early days, using select with no descriptors and a timeout value set appropriately.

If it's returning -1, you need to look at errno to see what the problem is.


Ah, based on your comment, WSAEINVAL means WinSock. That explains your problems. It's a hideous beast that should be put out of our misery :-)

This page here states that you will get WSAEINVAL if the time-out value is not valid or all three descriptor parameters are null. It later states that FD_ZERO "initialises the set to the null set". Whether that last snippet means that the parameter is still considered NULL, I don't know. Assuming that your timeout is okay (since it works if you have an FD in one of the sets), that's probably the case.

I do know that Berkeley sockets distinguishes between a NULL FD set (i.e., a NULL pointer) and and empty FD set (a valid pointer with no FDs set) - WinSock may not be that versatile especially since the nfds is ignored and included only for compatibility with Berkeley.

I suspect (although I'm not sure) that WinSock is probably expecting you to not do what you're trying to do. It may well just assume that you're doing something wrong and should use select only for one or more sockets in one or more FD sets (since the only use for select with no FDs is a delay and Windows has plenty of better ways of doing delays).

人海汹涌 2024-09-04 02:11:16

在 Windows 下,select 函数完全忽略 nfds 参数。
我目前无法对此进行测试,但如果所有集合均非 NULL 但为空,则可能会返回错误。

要解决此问题,您可以完全跳过选择,或者保留一个虚拟套接字以确保集合中始终至少有一个套接字。

Under Windows, the select function ignores the nfds argument entirely.
I can't test this currently, but it's possible that an error is returned if all sets are non-NULL but empty.

To work around this, you can either skip the select entirely, or keep a dummy socket to ensure that there is at least one socket in the set at all times.

可可 2024-09-04 02:11:16

请阅读选择帮助。如果“超时值无效,或者所有三个描述符参数都为空”,则将返回 WSAEINVAL。 。很明显,您还传递了不正确的超时值。因此,如果您想使用 0 个描述符,请提供正确的超时值。

Please read the select help. It says WSAEINVAL will be returned if "The time-out value is not valid, or all three descriptor parameters were null." . It's obvious you are passing an incorrect timeout value also. So, provide a correct timout value if you want to use with 0 descriptors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文