select()ing写入fd_set的概念

发布于 2024-10-03 21:46:16 字数 295 浏览 3 评论 0原文

根据我对 select() 的所有文档的理解,似乎可以使用写入 fd_set 上的 select() 来检查套接字(描述符)的可用性send()ing,以便可用于检测成功的非阻塞 connect() 尝试,但我不知道的是套接字何时变得不可用成功 connect()accept() 后? 理论上这是否意味着套接字始终可用于 send() ?

最后一个问题是,在整个会话中保持 select() 连接的套接字用于写操作是否实用?

谢谢。

From what I understood from all the documentations of select(), it seems that select()ing on a write fd_set could be used to check for socket (descriptor) availibility for send()ing so that could be used to detect a successful non-blocking connect() attempt, but what I don't get is when does a socket ever become unavailable after a successful connect() or accept()?
And does that in theory mean that the socket is always available for send()ing?

As a last question, is it practical to keep select()ing connected sockets in for write operations for the whole session?

Thanks.

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

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

发布评论

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

评论(3

像你 2024-10-10 21:46:17

使用非阻塞套接字时,任何操作都可能失败并出现 WSAEWOULDBLOCK 错误。在 connect() 和 send() 的情况下, select() 可用于确定待处理的 connect() 调用何时已成功连接到服务器,或者套接字何时可写,以便可以在不阻塞的情况下接受新数据。

When using a non-blocking socket, any operation can fail with an WSAEWOULDBLOCK error. In the case of connect() and send(), select() can be used to determine when a pending connect() call has successfully connected to the server, or when the socket is writable so it can accept new data without blocking.

绾颜 2024-10-10 21:46:17

“可用于发送”仅意味着套接字的发送缓冲区中有空间。大多数情况下都是如此,从连接完成并分配缓冲区开始。只有当缓冲区填满时,它才是不真实的,而只有当目标套接字的接收缓冲区填满时,才会发生这种情况,而只有当读取应用程序比写入程序慢时,才会发生这种情况。

'Available for sending' just means that there is room in the socket's send buffer. This is true most of the time, starting from when the connection completes and the buffer is allocated. It is only ever untrue when the buffer fills, which in turn only happens when the target socket's receive buffer fills, which only happens if the reading application is slower than the writer.

独夜无伴 2024-10-10 21:46:16

套接字无法写入的最常见情况是当连接通过相对较慢的网络链接时,您的应用程序可能会饱和。操作系统将缓冲有限数量的数据,因此您的应用程序必须在发送更多数据之前通过检查套接字是否可用(表明已发送一些数据并且缓冲区有可用空间)来约束自己。

至于您的另一个问题,如果单个线程使用任何类型的多个套接字(例如 Web 服务器的情况),那么使用 select() 来有效管理它们当然是有意义的。

The most common case of a socket being unavailable for writing is when the connection goes through a relatively slow network link that your application can saturate. The operating system will buffer a limited amount of data, so your application will have to restrain itself by checking if the socket is available (indicating that some data has been sent and the buffer has free space) before sending more.

As for your other question, if you have multiple sockets of any kind in use by a single thread, as is the case with e.g. a web server, it certainly makes sense to use select() in order to manage them efficiently.

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