多线程 IOCP 客户端问题
我正在编写一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非你这样做,否则这不是问题。
只要您不调用 SetFileCompletionNotificationModes() 并设置标志以在成功时跳过完成端口处理,然后即使 WSARecv(或其他)返回 SUCCESS,IO 完成数据包也会排队到 IOCP,就像返回 ERROR_IO_PENDING 一样。因此,您不需要对非错误返回情况进行特殊处理。
请参阅 http://support.microsoft.com/default.aspx ?scid=kb;en-us;Q192800 了解详细信息。
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.
首先,将调用分解为更清晰的逻辑:
此外,正如您在我的代码中看到的,您不应该根据 WSASend:
除此之外,您对 WSASend() 的调用看起来不错。
First of all break the call into more clear logic:
Also, as you can see in my code, you should NOT pass the "&bytesSent" parameter according to WSASend:
Besides that your call to WSASend() looks fine.