本文介绍了GetQueuedCompletionStatus永远阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写服务器应用程序,并且想使用IOCompletion端口,因此我为服务器编写了一个原型,但是我遇到了GetQueuedCompletionStatus的问题,该问题永不返回(阻塞).下面是我的代码:
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);
}
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()); } } }
有什么主意吗?预先感谢.
解决方案
可能的可能性:检查是否已将侦听器套接字与完成端口相关联(我自己犯了这个错误).如果没有,则GetQueuedCompletionStatus()将会永远阻塞.
I'm writing a server application and I want to use IOCompletion ports, so I wrote a prototype for the server, but I'm facing a problem with GetQueuedCompletionStatus that it never returns(it blocks). Below is my 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;
}
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);
}
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()); } } }
Any idea?thanks in advance.
解决方案
ONe possibility: Check if you have associated the listener socket with the completion port (made this mistake myself). If you haven't the GetQueuedCompletionStatus() will block forever.
这篇关于GetQueuedCompletionStatus永远阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!