1

我想在命名管道上异步接收数据。以下是我的服务器代码:

bool bContinue = true;
bool bMessageReceived = false;
int iMessage;
DWORD dwBytesRead = 0;
OVERLAPPED oReadOverlapped = {0};
oReadOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

while(bContinue)
{
    int result = ReadFile(hPipe, &iMessage, sizeof(int), NULL, &oReadOverlapped);
    int dwError = GetLastError();
    cout<< "readFile Error: " << dwError <<endl; 
    if( result )
        bMessageReceived = true;
    else
    {
       bool ret = GetOverlappedResult(hPipe, &oReadOverlapped, &dwBytesRead,                    
                     FALSE); 
       if( ret )
       {
           bMessageReceived = true;
       }
       else
       {
        dwError = GetLastError();
        cout<< "GetOverlappedResult Error: "<<dwError <<endl;
        bMessageReceived = !( !ret && dwError == ERROR_IO_INCOMPLETE);
       }
   }

   if( bMessageReceived )
   {
       ResetEvent(oReadOverlapped.hEvent);
       ProcessMessage(iMessage, hPipe);
   }

   bContinue = !IsServerStopped();
 }

客户端代码成功“同步”发送消息。但是每次服务器调用 readFile 时,它​​都会返回 ERROR_IO_PENDING 并且 GetOverlappedResult 会返回 ERROR_IO_INCOMPLETE。它继续循环。我正在使用 windows vista 和 Visual Stdio 2010。这段代码可能有什么问题。

问候,库拉姆。

4

0 回答 0