C Socket/Polling 错误地返回 POLLOUT 事件?

C Socket/Polling incorrectly returning POLLOUT event?

我正在尝试使用套接字编写端口打开测试,但出于某种原因,这会报告 "port open" 无效的 IP 地址。我目前连接到一个未连接到 Internet 的接入点,因此这错误地报告了为外部 IP 地址打开的端口。

首先我设置了套接字,因为这是非阻塞模式,它通常在第一个 if 语句之前仍在进行中。然后我正在轮询套接字。但是,对于外部 IP 上的套接字,我收到了 POLLOUT 事件,尽管这似乎不可能...

我错过了什么?为什么轮询收到的事件包含 POLLOUT? 我尝试在再次调用轮询之前重置 pollfd 结构,但这并没有改变结果。

result = connect(sd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));

if(result == 0) //woohoo! success
{
    //SUCCESS!
    return true;
}
else if (result < 0 && errno != EINPROGRESS) //real error
{
    //FAIL
    return false;
}

// poll the socket until it connects
struct pollfd fds[1];
fds[0].fd = sd;
fds[0].events = POLLOUT | POLLRDHUP | POLLERR | POLLNVAL;
fds[0].revents = 0;

while (1)
{
    result = poll(fds, 1, 1);

    if (result < 1)
    {
        //Poll failed, mark as FAIL
    }
    else
    {
        // see which event occurred
        if (fds[0].revents & POLLOUT || fds[0].revents & POLLRDHUP)
        {
           //SUCCESS
        }
        else if (fds[0].revents & POLLERR || fds[0].revents & POLLNVAL)
        {
            //FAIL
        }
    }
}

我需要在收到 POLLOUT 事件后检查 SO_ERROR - POLLOUT 本身并不表示成功。

//now read the error code of the socket
int errorCode;
uint len = sizeof(errorCode);
result = getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR,
               &errorCode, &len);