更多异步套接字问题:

发布于 2024-10-20 19:50:48 字数 687 浏览 1 评论 0原文

最初的问题此处

所以我一直在阅读异步套接字,并且还有更多问题问题。主要是混凝土。

1:我可以使用带有 select() 的阻塞套接字而不会产生任何影响,对吗?

2:当我使用 FD_SET() 时,我附加当前的 fd_set* 而不更改它,对吗?

3:当使用FD_CLR()时,我可以简单地传入我想要删除的套接字的套接字ID,对吧?

4:当我使用 FD_CLR() 删除套接字时,是否有重置最大文件描述符 (nfds) 的首选方法?

5:假设我将所有连接的套接字都放在一个向量中,当 select() 返回时,我可以遍历该向量并检查 if (FD_ISSET (theVector[loopNum], &readFileSet)) 看看是否需要读取任何数据,对吗?如果返回 true,我可以简单地使用我在同步套接字上使用的相同接收函数来检索该数据吗?

6:如果 select() 尝试从关闭的套接字读取数据会发生什么?我知道它返回 -1,但它是否设置了 errno 或者是否有其他方法可以继续使用 select()?

7:你为什么这么厉害? =D


感谢您的宝贵时间,抱歉让您头痛,希望您能提供帮助!

Initial questions here

So I've been reading up on asynchronous sockets, and I have a couple more questions. Mostly concrete.

1: I can use a blocking socket with select() without repercussions, correct?

2: When I use FD_SET() I'm appending the current fd_set* not changing it, correct?

3: When using FD_CLR(), I can simply pass in the socket ID of the socket I wish to remove, right?

4: When I remove a socket, using FD_CLR(), is there a prefferred way of resetting the Max File Descriptor (nfds)?

5: Say I have all of my connected sockets in a vector, when select() returns, I can just itterate through that vector and check if (FD_ISSET (theVector[loopNum], &readFileSet)) to see if any data needs to be read, correct? And if this returns true, I can simply use the same receiving function I was using on my synchronous sockets to retreive that data?

6: What happens if select() attempts to read from a closed socket? I know it returns -1, but does it set errno or is there some other way I can continue to use select()?

7: Why are you so awesome? =D


I appreciate your time, sorry for the headache, and I hope you can help!

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

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

发布评论

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

评论(2

鸩远一方 2024-10-27 19:50:48
  1. 不清楚? FD_SET 将套接字插入到集合中。如果套接字已经存在,则不会发生任何变化。
  2. FD_CLR 从集合中删除套接字,如果套接字不存在,则没有任何更改
  3. 您可以保留并行集合<>套接字,然后从中获取最高值。或者你可以设置一个布尔值“在下一个选择之前重新扫描nfd”(注意:在Windows上nfd被忽略)
  4. 正确
  5. 如果选择失败,快速修复是迭代套接字并在每个套接字上一一进行select()以查找假的。最好你的代码不应该允许 select() 在你已经关闭的套接字上,如果另一端关闭了它,那么选择就完全有效。
  6. 我需要让你和我的妻子谈谈。
  1. Yes
  2. Unclear? FD_SET inserts a socket into the set. If the socket is already there, nothing changes.
  3. FD_CLR removes a socket from the set, if the socket isn't there nothing's changed
  4. You could keep a parallel set<> of sockets, then get the highest value from there. Or you could just set a bool saying "rescan for nfd before next select" (NOTE: On windows nfd is ignored)
  5. Correct
  6. If select fails, the quick fix is to iterate sockets and select() on each of them one by one to find the bogus one. Optimally your code should not allow select() on a socket you have closed though, if the other end closed it it's perfectly valid to select on.
  7. I need to get you to talk to my wife.
那伤。 2024-10-27 19:50:48

所以我一直在阅读异步套接字

从接下来的内容来看,我认为你没有。您似乎一直在阅读有关非阻塞套接字的内容。不是同一件事。

1:我可以使用带有 select() 的阻塞套接字而不会产生任何影响,对吗?

不。考虑这样一种情况:侦听套接字变得可读,表明即将发生 accept(),但同时客户端关闭连接。如果您随后调用 accept(),您将阻塞,直到下一个传入连接,从而阻止您为其他套接字提供服务。

2:当我使用 FD_SET() 时,我会附加当前的 fd_set* 而不更改它,对吗?

不,你正在设置一点。如果已经设置,则不会发生任何变化。

3:当使用FD_CLR()时,我可以简单地传入我想要删除的套接字的套接字ID,对吗?

正确的。

4:当我使用 FD_CLR() 删除套接字时,是否有重置最大文件描述符 (nfds) 的首选方法?

不是真的,只是重新扫描并重新计算。但您实际上并不需要重置它。

5:假设我将所有连接的套接字都放在一个向量中,当 select() 返回时,我可以遍历该向量并检查 if (FD_ISSET (theVector[loopNum], &readFileSet)) 以查看是否有任何需要读取数据,对吗?

正确,但更常见的是迭代 FD 集本身。

如果返回 true,我可以简单地使用我在同步套接字上使用的相同接收函数来检索该数据?

在您的阻塞套接字上,是的。

6:如果 select() 尝试从关闭的套接字读取数据会发生什么?

select() '尝试从关闭的套接字读取数据。它可能会尝试在关闭的套接字上选择,在这种情况下,它将返回 -1 且 errno == EBADF,如文档中所述。

我知道它返回 -1,但它是否设置了 errno 或者是否有其他方法可以继续使用 select()?

见上文。

So I've been reading up on asynchronous sockets

Judging by what follows I don't think you have. You appear to have been reading about non-blocking sockets. Not the same thing.

1: I can use a blocking socket with select() without repercussions, correct?

No. Consider the case where a listening socket becomes readable, indicating an impending accept(), but meanwhile the client closes the connection. If you then call accept() you will block until the next incoming connection, preventing you from servicing other sockets.

2: When I use FD_SET() I'm appending the current fd_set* not changing it, correct?

No. You are setting a bit. If it's already set, nothing changes.

3: When using FD_CLR(), I can simply pass in the socket ID of the socket I wish to remove, right?

Correct.

4: When I remove a socket, using FD_CLR(), is there a preferred way of resetting the Max File Descriptor (nfds)?

Not really, just re-scan and re-compute. But you don't really need to reset it actually.

5: Say I have all of my connected sockets in a vector, when select() returns, I can just itterate through that vector and check if (FD_ISSET (theVector[loopNum], &readFileSet)) to see if any data needs to be read, correct?

Correct, but it's more usual just to iterate through the FD set itself.

And if this returns true, I can simply use the same receiving function I was using on my synchronous sockets to retreive that data?

On your blocking sockets, yes.

6: What happens if select() attempts to read from a closed socket?

select() doesn't 'attempt to read from a closed socket. It may attempt to select on a closed socket, in which case it will return -1 with errno == EBADF, as stated in the documentation.

I know it returns -1, but does it set errno or is there some other way I can continue to use select()?

See above.

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