int rc=poll(fds,1,-1);让我们假设远程对等机宕机。插座在这里坏了。在这种情况下,轮询系统调用将返回-1或将返回>0,并将断开连接报告为FD上的错误事件。
此外,轮询在0超时时会返回什么。int rc=轮询(fds,nfds,0);
没有。它所知道的只是套接字上发生了一些事情,以及是读取事件、写入事件还是错误事件。对等断开连接算作读取事件。
如果套接字断开,poll()
将返回>0,则必须检查recv
的返回值才能知道套接字是否断开(在这种情况下,recv()
将返回0)。
此外,轮询在0超时时会返回什么。int rc=轮询(fds,0);
poll()
会立即返回,返回值可以>=0。
你真的应该阅读poll()
手册页,这里有你需要知道的一切。
来自手册页:
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字段将填充适当的值。例如POLLERR。