命名管道上的异步读操作?

时间:2013-10-08 03:06:17

标签: c++ windows windows-xp named-pipes overlapped-io

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

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。 这段代码可能会出现什么问题。

此致 Khurram。

0 个答案:

没有答案