poll 系统调用是否知道远程套接字是否关闭或断开连接?

问题描述 投票:0回答:3

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

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

c sockets tcp network-programming polling
3个回答
4
投票

不,没有。它只知道套接字上发生了某些事情,以及是读取事件、写入事件还是错误事件。对等断开连接算作读取事件。


3
投票

如果套接字断开,

poll()
将返回> 0,那么您必须检查
recv
的返回值,以了解套接字是否已断开连接(在这种情况下,
recv()
将返回0)。

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

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

您确实应该阅读

poll()
手册页,这里有您需要了解的所有内容。


-2
投票

来自man页面:

字段 revents 是一个输出参数,由内核用事件填充 这确实发生了。 revents 中返回的位可以包括以下任何一个 在事件中指定,或者值 POLLERR、POLLHUP 或 POLLNVAL 之一。 (这三位在事件字段中无意义,将在 每当相应的条件为真时,revent 字段。)

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。

© www.soinside.com 2019 - 2024. All rights reserved.