更多异步套接字问题:
最初的问题此处
所以我一直在阅读异步套接字,并且还有更多问题问题。主要是混凝土。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从接下来的内容来看,我认为你没有。您似乎一直在阅读有关非阻塞套接字的内容。不是同一件事。
不。考虑这样一种情况:侦听套接字变得可读,表明即将发生
accept()
,但同时客户端关闭连接。如果您随后调用accept()
,您将阻塞,直到下一个传入连接,从而阻止您为其他套接字提供服务。不,你正在设置一点。如果已经设置,则不会发生任何变化。
正确的。
不是真的,只是重新扫描并重新计算。但您实际上并不需要重置它。
正确,但更常见的是迭代 FD 集本身。
在您的阻塞套接字上,是的。
select()
不 '尝试从关闭的套接字读取数据。它可能会尝试在关闭的套接字上选择,在这种情况下,它将返回 -1 且errno == EBADF
,如文档中所述。见上文。
Judging by what follows I don't think you have. You appear to have been reading about non-blocking sockets. Not the same thing.
No. Consider the case where a listening socket becomes readable, indicating an impending
accept()
, but meanwhile the client closes the connection. If you then callaccept()
you will block until the next incoming connection, preventing you from servicing other sockets.No. You are setting a bit. If it's already set, nothing changes.
Correct.
Not really, just re-scan and re-compute. But you don't really need to reset it actually.
Correct, but it's more usual just to iterate through the FD set itself.
On your blocking sockets, yes.
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 witherrno == EBADF
, as stated in the documentation.See above.