GetQueuedCompletionStatus 永远阻塞

发布于 2024-12-10 18:42:52 字数 2237 浏览 2 评论 0原文

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

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());

    }

  }
}

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

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.

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

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

发布评论

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

评论(2

北城挽邺 2024-12-17 18:42:52

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

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

Windows Sockets 2.0:使用完成端口编写可扩展的 Winsock 应用程序

You are not using the Completion Port correctly, so it has nothing to do, and thus no status to report. Using a Completion Port with sockets is a two-step process, but you are only doing half of the steps.

Read the following MSDN article for details:

Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports

找回味觉 2024-12-17 18:42:52

一种可能性:检查您是否已将侦听器套接字与完成端口相关联(我自己也犯过这个错误)。如果没有,GetQueuedCompletionStatus() 将永远阻塞。

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.

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