C++中的NamedPipeClientStream StreamReader问题

发布于 2024-08-09 07:58:37 字数 521 浏览 0 评论 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();

When reading from a NamedPipes server using the .net NamedPipeClientStream class I can only get the data on the first read in C++, every time it's just an empty string. In c# it works every time.

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左岸枫 2024-08-16 07:58:37

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

"Hello World!\n\0"

在第一次传递时,这将发送

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

"\0Hello World!\n"

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

The problem was related to C++ null terminator. The NamedPipes Server was sending for example

"Hello World!\n\0"

On the first pass this would send

"Hello World!\n" leaving \0 in the pipe. On subsequents sends it would tranmit

"\0Hello World!\n"

C# would get the whole string while c++ would terminate the string at the \0 char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文