0

int rc = poll(fds, 1, -1); 让我们说远程对等体出现故障。插座在这里中断。在这种情况下,poll 系统调用将返回 -1 或将返回 > 0 并将断开连接报告为 FD 上的错误事件。

此外,什么会在 0 超时时轮询返回。int rc = poll(fds, nfds, 0);

4

3 回答 3

3

不,它没有。它所知道的只是套接字上发生了一些事情,以及那是读取事件、写入事件还是错误事件。对等断开计为读取事件。

于 2013-07-17T10:16:39.283 回答
2

如果套接字中断,poll()将返回 > 0,那么您必须检查返回值recv才能知道套接字是否断开(recv()在这种情况下将返回 0)。

此外,什么会在 0 超时时轮询返回。int rc = poll(fds, nfds, 0);

poll()将立即返回,返回值可以>= 0。

你真的应该阅读手册页,你需要知道的都有poll()

于 2013-07-17T09:14:07.227 回答
-2

从手册页:

The field revents is an output parameter, filled by the kernel with the events 
that actually occurred. The bits returned in revents can include any of those 
specified in events, or one of the values POLLERR, POLLHUP, or POLLNVAL. 
(These three bits are meaningless in the events field, and will be set in the 
revents field whenever the corresponding condition is true.)

If none of the events requested (and no error) has occurred for any of the file
descriptors, then poll() blocks until one of the events occurs.

Return Value
On success, a positive number is returned; this is the number of structures which 
have nonzero revents fields (in other words, those descriptors with events or 
errors reported). A value of 0 indicates that the call timed out and no file 
descriptors were ready. On error, -1 is returned, and errno is set appropriately. 

因此,很明显,如果 FD 返回值 > 0 中存在错误,并且 revents 字段填充了适当的值。例如轮询。

于 2013-07-17T09:14:43.973 回答