C++中的NamedPipeClientStream StreamReader问题
当使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题与 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.