0

当使用 .net NamedPipeClientStream类从 NamedPipes 服务器读取数据时,我只能在 C++ 中第一次读取时获取数据,每次它只是一个空字符串。在 c# 中,它每次都有效。

pipeClient = gcnew NamedPipeClientStream(".", "Server_OUT", PipeDirection::In);

try
{
    pipeClient->Connect();
}
catch(TimeoutException^ e)
{
    // swallow
}

StreamReader^ sr = gcnew StreamReader(pipeClient);
String^ temp;
while (temp = sr->ReadLine())
{
    // = sr->ReadLine();
    Console::WriteLine("Received from server: {0}", temp);
}
sr->Close();
4

1 回答 1

0

该问题与 C++ 空终止符有关。例如,NamedPipes 服务器正在发送

“你好,世界!\n\0”

在第一遍这将发送

"Hello World!\n"在管道中留下\0 。在随后的发送中,它会传输

"\0你好世界!\n"

C# 将获取整个字符串,而 c++ 将在\0字符处终止字符串。

于 2009-11-05T12:09:46.963 回答