多线程 IOCP 客户端问题

发布于 2024-09-04 19:39:01 字数 1421 浏览 5 评论 0原文

我正在编写一个使用 IO 完成端口的多线程客户端。

我创建并连接设置了 WSA_FLAG_OVERLAPPED 属性的套接字。

if ((m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
    throw std::exception("Failed to create socket.");
}

if (WSAConnectByName(m_socket, L"server.com", L"80", &localAddressLength, reinterpret_cast<sockaddr*>(&localAddress), &remoteAddressLength, &remoteAddress, NULL, NULL) == FALSE)
{
    throw std::exception("Failed to connect.");
}

我将 IO 完成端口与套接字关联起来。

if ((m_hIOCP = CreateIoCompletionPort(reinterpret_cast<HANDLE>(m_socket), m_hIOCP, NULL, 8)) == NULL)
{
    throw std::exception("Failed to create IOCP object.");
}

一切似乎都很顺利,直到我尝试通过套接字发送一些数据。

SocketData* socketData = new SocketData;
socketData->hEvent = 0;

DWORD bytesSent = 0;
if (WSASend(m_socket, socketData->SetBuffer(socketData->GenerateLoginRequestHeader()), 1, &bytesSent, NULL, reinterpret_cast<OVERLAPPED*>(socketData), NULL) == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING)
{
    throw std::exception("Failed to send data.");
}

WSASend 立即返回,而不是返回 SOCKET_ERROR 并将最后一个错误设置为 WSA_IO_PENDING。

我需要 IO 挂起,并在我的线程函数(也是我的工作线程)中处理它的完成。

unsigned int __stdcall MyClass::WorkerThread(void* lpThis)
{

}

我以前曾经这样做过,但我不知道在这种情况下出了什么问题,我非常感谢任何帮助我解决这个问题的努力。

I am writing a multithreaded client that uses an IO Completion Port.

I create and connect the socket that has the WSA_FLAG_OVERLAPPED attribute set.

if ((m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
    throw std::exception("Failed to create socket.");
}

if (WSAConnectByName(m_socket, L"server.com", L"80", &localAddressLength, reinterpret_cast<sockaddr*>(&localAddress), &remoteAddressLength, &remoteAddress, NULL, NULL) == FALSE)
{
    throw std::exception("Failed to connect.");
}

I associate the IO Completion Port with the socket.

if ((m_hIOCP = CreateIoCompletionPort(reinterpret_cast<HANDLE>(m_socket), m_hIOCP, NULL, 8)) == NULL)
{
    throw std::exception("Failed to create IOCP object.");
}

All appears to go well until I try to send some data over the socket.

SocketData* socketData = new SocketData;
socketData->hEvent = 0;

DWORD bytesSent = 0;
if (WSASend(m_socket, socketData->SetBuffer(socketData->GenerateLoginRequestHeader()), 1, &bytesSent, NULL, reinterpret_cast<OVERLAPPED*>(socketData), NULL) == SOCKET_ERROR && WSAGetLastError() != WSA_IO_PENDING)
{
    throw std::exception("Failed to send data.");
}

Instead of returning SOCKET_ERROR with the last error set to WSA_IO_PENDING, WSASend returns immediately.

I need the IO to pend and for it's completion to be handled in my thread function which is also my worker thread.

unsigned int __stdcall MyClass::WorkerThread(void* lpThis)
{

}

I've done this before but I don't know what is going wrong in this case, I'd greatly appreciate any efforts in helping me fix this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事还在继续 2024-09-11 19:39:01

It's not a problem unless you make it so.

As long as you're not calling SetFileCompletionNotificationModes() and setting the flag to skip completion port processing on success then even if WSARecv (or whatever) returns SUCCESS an IO Completion Packet is queued to the IOCP the same as if ERROR_IO_PENDING was returned. Thus you need no special handling for the non error return case.

See http://support.microsoft.com/default.aspx?scid=kb;en-us;Q192800 for details.

坠似风落 2024-09-11 19:39:01

首先,将调用分解为更清晰的逻辑:

int nRet = WSASend(m_socket, socketData->SetBuffer(socketData->GenerateLoginRequestHeader()), 1, NULL, NULL, reinterpret_cast<OVERLAPPED*>(socketData), NULL);
if (nRet == SOCKET_ERROR)
{
    if ((WSAGetLastError()) == WSA_IO_PENDING)
        nRet = 0; // ok
    else
        throw std::exception("Failed to send data."); // failed
}

此外,正如您在我的代码中看到的,您不应该根据 WSASend

如果该参数使用 NULL
lpOverlapped 参数不为 NULL
避免潜在的错误结果。

除此之外,您对 WSASend() 的调用看起来不错。

First of all break the call into more clear logic:

int nRet = WSASend(m_socket, socketData->SetBuffer(socketData->GenerateLoginRequestHeader()), 1, NULL, NULL, reinterpret_cast<OVERLAPPED*>(socketData), NULL);
if (nRet == SOCKET_ERROR)
{
    if ((WSAGetLastError()) == WSA_IO_PENDING)
        nRet = 0; // ok
    else
        throw std::exception("Failed to send data."); // failed
}

Also, as you can see in my code, you should NOT pass the "&bytesSent" parameter according to WSASend:

Use NULL for this parameter if the
lpOverlapped parameter is not NULL to
avoid potentially erroneous results.

Besides that your call to WSASend() looks fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文