如果远程套接字关闭或断开,是否轮询系统调用?

时间:2013-07-17 06:17:40

标签: c sockets tcp network-programming polling

int rc = poll(fds,1,-1); 让我们说远程同伴失败了。 socket在这里打破。 在这种情况下,poll系统调用将返回-1或 将返回> 0并在FD上将断开连接报告为错误事件。

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

3 个答案:

答案 0 :(得分:3)

不,不。它只知道套接字上发生了一些事情,无论是读取事件,写入事件还是错误事件。对等断开计为读事件。

答案 1 :(得分:1)

如果套接字断开,poll()将返回> 0,那么你必须检查recv的返回值,以便知道套接字是否断开连接(在这种情况下recv()将返回0)。

  

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

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

你真的应该阅读the poll() manpage,你需要知道的全部内容。

答案 2 :(得分:-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字段填充适当的值。例如POLLERR。