gpt4 book ai didi

c++ - C 套接字/轮询错误地返回 POLLOUT 事件?

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:03 25 4
gpt4 key购买 nike

我正在尝试使用套接字编写端口打开测试,出于某种原因,这会报告无效 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);

关于c++ - C 套接字/轮询错误地返回 POLLOUT 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38315918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com