开发者

NamedPipeClientStream StreamReader problem in C++

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 开发者_StackOverflow社区from server: {0}", temp);
}
sr->Close();


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜