2013-07-17 93 views
0

int rc = poll(fds,1,-1); 让我们说远程同伴关闭。 socket在这里打破。 在这种情况下,轮询系统调用返回-1或 将返回> 0,并将断开连接报告为FD上的错误事件。是否知道远程套接字是关闭还是断开?

而且,你会轮询0超时回报。 int rc = poll(fds,nfds,0);

+0

当你尝试过时发生了什么? –

回答

1

如果插座休息,poll()将返回> 0,那么你将要检查的recv的返回值,以便知道如果套接字断开(recv()会在这种情况下返回0)。

而且,你会轮询0超时回报。 int rc = poll(fds,nfds, 0);

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

你真的应该读the poll() manpage,有所有你需要知道的。

+0

在0超时时,如果没有事件,则返回值为0,如果在该时刻有任何事件,则返回1,是否正确? – Medicine

+1

@Medicine这几乎是真的:)'poll()'的返回值是事件的数量。因此,如果在那一刻有** 1 **事件,poll()将返回1,但如果有多于1个事件,则返回值可能大于1(所以如果没有事件返回值为0事件)。 – nouney

+1

@医学这没有意义。超时只发生在没有事件的情况下。如果有事件没有超时。 – EJP

-1

从手册页:

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例如。

+0

如果FD [返回值> 0'中存在错误,它不会'清除[说]。它说,如果连接上有一个传入的错误事件,返回值是>零。有一个区别。并且由同伴有序地关闭并不是一个错误,而是一个阅读事件。 – EJP

2

不,它不。它只知道在套接字上发生了什么,以及它是一个读事件,一个写事件还是一个错误事件。对等方断开连接计为读取事件。

相关问题