无法理解 select() 中 write_fds 的使用 - c

发布于 2024-11-09 01:41:43 字数 388 浏览 0 评论 0原文

我正在编写一个能够向客户端接收和发送消息的服务器。 我使用 select() 来跟踪所有连接的客户端。

select(max_fd+1,&read_fd,&write_fd,NULL,NULL);

我确实了解 select 中 read_fds 的使用 - 如果某个客户端向我发送数据或者尝试连接到服务器, select 将唤醒,并且 read_fd 将包含我可以从中读取数据的所有 fd。 但是,我无法理解 write_fds 的用法:
我不知道 select 将如何以及何时醒来,因为服务器程序中 write_fd 的更改(因此我认为我不需要使用它)。

我将感谢所有澄清 write_fd 用法的人。谢谢!

I'm writing a server that has the ability to receive and send msgs to clients.
I'm using select() to keep track of all connected clients.

select(max_fd+1,&read_fd,&write_fd,NULL,NULL);

I do understand the use of the read_fds in select - if some client sends me data or trying to connect to the server, select will wake up, and the read_fd will contain all the fd's that I can read data from them.
however, I'm having trouble understanding the use of the write_fds:
I don't see how and when select will wake up because of a change in write_fd in the server program (and thus I think I don't need to use it).

I will thank everyone who will clarify the use of write_fd. Thanks!

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

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

发布评论

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

评论(4

风启觞 2024-11-16 01:41:44

如果您使用非阻塞套接字,写入集将告诉您套接字何时准备好发送更多数据。

If you use non-blocking sockets, the write set will tell you when a socket is ready to send more data.

我的痛♀有谁懂 2024-11-16 01:41:44

所有这些复杂、令人困惑的套接字业务在书中都得到了非常清晰和完整的解释:

W. Richard Stevens、Bill Fenner 和 Andrew M. Rudoff,Unix 网络编程,卷 1:套接字网络 API(第 3 版)

如果您可以在库中找到副本,您将在前面获得完整的信息你。此外,还会有一些示例代码可能非常接近您想要理解的代码。

华泰

All of this complex, confusing sockets business is explained very clearly and completely in the book:

W. Richard Stevens, Bill Fenner, and Andrew M. Rudoff, Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition)

If you can find a copy in the library, you'll have the complete info right in front of you. Moreover, there will be sample code that probably follows very closely the code you're trying to understand.

HTH

爱已欠费 2024-11-16 01:41:43

使用 TCP 套接字时,发送的任何数据都必须存储在套接字缓存中,直到远程端确认已收到数据(通过 ACK 数据)。如果您发送数据的速度快于远程端消耗数据的速度,您最终将填满缓冲区。此时,如果您使用阻塞套接字,则发送将阻塞,直到数据可以放入缓冲区为止。此时,套接字(文件描述符)不可写,并且 select 将阻塞,直到缓冲区有可用于写入的空间。

When using TCP sockets any data sent must be stored in the socket cache until the remote end has confirmed that it has received the data (by ACKing the data). If you are sending data faster than the remote end is consuming the data, you will eventually fill the buffer. At this point, if you are using a blocking socket, the send will block until the data can be placed in the buffer. At this point, the socket (file descriptor) is not writable and the select will block until the buffer has space available for writing.

稀香 2024-11-16 01:41:43

很简单,当你向客户端发送响应时,可能会出现这样的情况:
尚无法接收更多数据(带宽/响应时间等)。

在这种情况下,写操作可能会阻塞。

因此,在写入套接字之前,您需要检查套接字的“可写性”。

请注意,如果您使用非阻塞套接字,则仅当您在写入操作中收到 EAGAIN 或 EWOULDBLOCK 错误时才应使用它,当使用阻塞套接字时,您应该
在发送数据之前,请始终确保套接字可写(使用 write_fds)。

Very simply, when you send a response to client, there may be a situation when it
does not able yet to receive more data (bandwidth/response time and so on).

In such cases write operation may block.

Thus you need to check for "writability" of the socket before you write into it.

Note if you use nonblocking sockets you should use it only when you receive EAGAIN or EWOULDBLOCK error on write operation, when using blocking sockets you should
always make sure that the socket is writeable (using write_fds) before you send the data.

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