2

我在我的应用程序中使用 select() 调用的方式与此处使用的方式相同:beej.us/guide/bgnet/examples/selectserver.c

也就是说,我正在调用select(),循环遍历所有文件描述符来处理数据,然后再次调用select()。但是,我想知道在循环和处理数据时到达的数据会发生什么?select() 会立即返回,还是会等到它看到新数据?

问题的核心是,当recv() 有可用(旧)数据时,select 是立即返回,还是等待新数据到达?

4

2 回答 2

1

如果有可用数据,则 select() 立即返回。它不会等待更多数据到达。如果没有数据,则它会阻塞并等待。

在某种程度上,这必须是真的,因为带有 O_NONBLOCK 标志的文件描述符永远不会阻塞,但 select() 必须仍然有效。

于 2013-07-09T00:29:34.130 回答
1

select informs you if there is data to be read (in this case) on the socket. It does not care when it arrived, only if it's there.

So you will get a return indication from select regardless. In a similar manner, you may find that if you're notified on a socket when it has 5 bytes, and another 5 bytes appear between the select returning and you calling read. In that case, you get 10 bytes but that's fine. It won't "carry" those 5 bytes over to the next select.

于 2013-07-09T00:31:22.590 回答