全部 :)
我有一些代码可以在 Linux 和 WinXP 上正常运行
int FlowTestIP::do_recvfrom()
{
int ret;
struct timeval timeout;// = {2, 0};
timeout.tv_sec = 2;
timeout.tv_usec = 0;
fd_set rfds;
while(running) {
FD_ZERO(&rfds);
FD_SET(m_socket, &rfds);
ret = select(m_socket + 1, &rfds, 0, 0, &timeout);
cout << "// select ret = " << ret << " (errno = " << errno << ")" << endl;
if (ret == -1 || ret == 0) {
if (!(ret == 0 && errno == 0))
cout << "select ret = " << ret << " (errno = " << errno << ")" << endl;
return ret;
}
if (FD_ISSET(m_socket, &rfds)) {
cout << WSAGetLastError() << endl;
break;
}
}
return recvfrom(m_socket, in_buf, mtu, 0, (struct sockaddr *)&si_other, (socklen_t *)&otherAddrSize);
}
但在 Windows 7 中,即使退出客户端应用程序也会继续循环。错误的输出是
...
// 选择 ret = 1 (errno = 0)
0
// 选择 ret = 1 (errno = 0)
0
// 选择 ret = 1 (errno = 0)
0
FTControlServerThread::run
FTControlServerThread::
recieved pkt-> ft_tos = UCHAR_MAX
QFuture::waitForFinished()
client alive true
// 选择 ret = 1 (errno = 0)
0
// 选择 ret = 1 (errno = 0)
0
// 选择 ret = 1 (errno = 0)
0
normaly(在 WinXP 和 Linux 上)输出将是
...
// 选择 ret = 1 (errno = 0)
0
// 选择 ret = 1 (errno = 0)
0
// 选择 ret = 1 (errno = 0)
0
FTControlServerThread::run
FTControlServerThread::
recieved pkt-> ft_tos = UCHAR_MAX
QFuture::waitForFinished()
客户端存活 true
QFuture::finished
这就是我所期待的。
有没有人遇到过这样的不当行为?
提前致谢!
PS 正如@JoachimPileborg 要求展示我如何调用此函数:
FlowTest::recv(char *payload, size_t size)
{
...
while (running) {
len = do_recvfrom(); // it virtual method of class FlowTest
if (len == -1 || len == 0) {
return len;
}
if (running && !transport_recv_helper()) {
continue;
} else {
break;
}
}
...
}
从服务器 GUI 调用 recv
void FlowTestServer::runTest(FlowTestServerHelper handler)
{
...
if ((ret = server->recv(rcvdData, (size_t *)&size)) == -1 || ret == 0 || size == 0) {
if (server_busy == 0)
break;
cout << "can't receive data (size == " << size << "; ret = " << ret << ")" << endl;
continue;
}
...
}