gpt4 book ai didi

sockets - GetQueuedCompletionStatus 永远阻塞

转载 作者:行者123 更新时间:2023-12-03 11:53:19 33 4
gpt4 key购买 nike

我正在编写一个服务器应用程序,我想使用 IOCompletion 端口,所以我为服务器编写了一个原型(prototype),但我遇到了 GetQueuedCompletionStatus 的问题,它永远不会返回(它会阻塞)。下面是我的代码:



<pre><code>bool CreateSocketOverlappedServer()
{
WSADATA wsaData;
SOCKADDR_IN sockaddr;

if(WSAStartup(MAKEWORD(2,2,),&wsaData)){
_tprintf(_T("Unable to start up\n"));
return false;
}

SrvSocket = WSASocket(AF_INET,SOCK_STREAM,0,NULL,NULL,WSA_FLAG_OVERLAPPED);
if(SrvSocket==INVALID_SOCKET){
_tprintf(_T("Unable to start socket\n"));
return false;
}

sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(10000);
sockaddr.sin_addr.s_addr = INADDR_ANY;

/* now bind the socket */
if(bind(SrvSocket, (SOCKADDR *)&sockaddr, sizeof(SOCKADDR_IN))==SOCKET_ERROR){
_tprintf(_T("Unable to bind socket\n"));
return false;
}

if(listen(SrvSocket, 5)==SOCKET_ERROR){
_tprintf(_T("Error listening\n"));
return false;
}

return true;
}
</code></pre>



<pre><code>void WorkerThread(void *arg)
{
bool bret= false;
DWORD dwTransferedBytes=0;
CLIENTS *client;
PPER_IO_OPERATION_DATA data;

/* Just sleep for now */
while(true){
_tprintf(_T("Entering while\n"));
bret = GetQueuedCompletionStatus(hIocp,&dwTransferedBytes,(PULONG_PTR)&client,(LPOVERLAPPED *) &data,INFINITE);
if(!bret){
_tprintf(_T("Unable to process completion port\n"));
}
}
//Sleep(10000);
}
</code></pre>

<p>
</p>

void AcceptClientConnections(void *arg)
{
SOCKET ClientSocket;
CLIENTS *c;

_tprintf(_T("Start accepting client connections\n"));


while(true){
ClientSocket = accept(SrvSocket, NULL,NULL);

if(ClientSocket==INVALID_SOCKET){
_tprintf(_T("Unable to accept connection\n"));
continue;
}

/* do an association with completion port */
c = (CLIENTS *)malloc(sizeof(CLIENTS));
c->sock = ClientSocket;

/* associate with completion port */
if(!CreateIoCompletionPort((HANDLE)ClientSocket, hIocp, (ULONG_PTR)c,0)){
_tprintf(_T("Unable to associate with completion port\n: %d"),GetLastError());

}

}
}

有什么想法吗?提前致谢。

最佳答案

您没有正确使用完成端口,因此它无关紧要,因此也没有要报告的状态。将完成端口与套接字一起使用是一个两步过程,但您只完成了一半的步骤。

有关详细信息,请阅读以下 MSDN 文章:

Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports

关于sockets - GetQueuedCompletionStatus 永远阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7813549/

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