簡體   English   中英

Windows 命名管道部分讀取

[英]Windows named pipe partial read

當我通過 ReadFile 讀取管道並返回部分數據時,是否可能出現這種情況,導致我再次調用 ReadFile 直到達到指定的讀取字節數?

PIPE_TYPE_BYTE設置在創建命名管道的一側。

在讀取端,設置PIPE_READMODE_BYTE模式以讀取部分數據。

在部分讀取模式下,您可以使用PIPE_READMODE_BYTEPIPE_NOWAIT組合來避免ReadFile函數掛起。 lpNumberOfBytesRead參數可用於檢測是否有更多數據要讀取。 當沒有要讀取的數據時,如果ReadFile函數調用成功, lpNumberOfBytesRead將為零。 否則,通過調用 GetLastError 函數檢查錯誤。

基於官方示例: 命名管道客戶端多線程管道服務器

客戶端閱讀相關代碼將更改為:

dwMode = PIPE_READMODE_BYTE | PIPE_NOWAIT;// PIPE_READMODE_MESSAGE;
fSuccess = SetNamedPipeHandleState(
    hPipe,    // pipe handle 
    &dwMode,  // new pipe mode 
    NULL,     // don't set maximum bytes 
    NULL);    // don't set maximum time 

// ...

WCHAR rdWChar;
do
{
    // Read from the pipe. 

    fSuccess = ReadFile(
        hPipe,    // pipe handle 
        &rdWChar,  // buffer to receive reply 
        2,        // test size, read two bytes per read operation
        &cbRead,  // number of bytes read 
        NULL);    // not overlapped 

    if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
        break;

    wprintf(L"%c", rdWChar);
} while (cbRead); // repeat loop if there is more bytes 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM