views:

259

answers:

1

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

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.

Chris Porter