[英]Windows named pipe partial read
当我通过 ReadFile 读取管道并返回部分数据时,是否可能出现这种情况,导致我再次调用 ReadFile 直到达到指定的读取字节数?
PIPE_TYPE_BYTE
is set at side which create the named pipe. PIPE_TYPE_BYTE
设置在创建命名管道的一侧。
At reading side, set PIPE_READMODE_BYTE
mode for reading partial data.在读取端,设置
PIPE_READMODE_BYTE
模式以读取部分数据。
In partial reading mode, you can use PIPE_READMODE_BYTE
and PIPE_NOWAIT
combination to avoid the ReadFile
function hangs.在部分读取模式下,您可以使用
PIPE_READMODE_BYTE
和PIPE_NOWAIT
组合来避免ReadFile
函数挂起。 lpNumberOfBytesRead
parameter can be used to detect if there is more data to read. lpNumberOfBytesRead
参数可用于检测是否有更多数据要读取。 When there is no data to be read the lpNumberOfBytesRead
will be zero if the ReadFile
function call success.当没有要读取的数据时,如果
ReadFile
函数调用成功, lpNumberOfBytesRead
将为零。 Otherwise, check the error by calling the GetLastError function.否则,通过调用 GetLastError 函数检查错误。
Based on official sample: Named Pipe Client and Multithreaded Pipe Server .基于官方示例: 命名管道客户端和多线程管道服务器。
The client reading related code will be change to:客户端阅读相关代码将更改为:
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.