2011-05-18 139 views
2

我卡在這裏,請幫助。 我已經命名管道服務器C#,管道創建:WriteFile()塊(通過命名管道從C++客戶端寫入C#服務器)

new NamedPipeServerStream(pipeName, PipeDirection.InOut, numThreads); 

在C++中,我創建客戶端這樣的:

 m_hPipe = CreateFile( 
     strPipeName,   // Pipe name 
     GENERIC_READ | GENERIC_WRITE, // Read and write access 
     0,    // No sharing 
     NULL,    // Default security attributes 
     OPEN_EXISTING,   // Opens existing pipe 
     FILE_FLAG_OVERLAPPED,  
     NULL); 

我管類型設置爲PIPE_READMODE_BYTE | PIPE_TYPE_BYTE
我寫了一個函數WriteString()將一個字符串寫入管道。功能大致是這樣的:

// Write the length of the string to the pipe (using 2 bytes) 
    bool bResult = WriteFile(m_hPipe, buf, 2, &cbBytesWritten, NULL); 

    // Write the string itself to the pipe 
    bResult = WriteFile(m_hPipe, m_chSend, len, &cbBytesWritten, NULL); 

    // Flush the buffer 
    FlushFileBuffers(m_hPipe); 

我做了兩次調用函數:

WriteString(_T("hello server!")); // message sent and the server saw it correctly 
    WriteString(_T("goodbye server!")); // message not sent, coz WriteFile() blocked here 

現在的問題是:該程序被阻止在第二WriteString(第一的WriteFile()調用)電話。 只有當管道被服務器關閉時,WriteFile()調用纔會返回錯誤。

這是可靠的重現性。

什麼是在這裏阻塞WriteFile()調用?我已經在使用OVERLAPPED文件標誌。 管道緩衝區已滿?我一直從服務器端的管道讀取數據。

非常感謝!

回答

3

FILE_FLAG_OVERLAPPED啓用異步I/O。它不會自動使任何操作異步,非阻塞或緩衝。

您需要使用第5個參數WriteFile - 傳遞OVERLAPPED結構,可以在完成時設置事件或將文件句柄與I/O Completion Port關聯。

相關問題