0

I create a named pipe like so:

var server = new NamedPipeServerStream(pipename, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

Then, after a client connects, I create a StreamReader:

namedPipeReader = new StreamReader(server);

I can use the namedPipeReader to read lines from the stream, but then I need to pass it to an object that reads binary data.

I pass namedPipeReader.BaseStream, and the object fails to read anything. If I call

namedPipeReader.BaseStream.Read(bytes, 0, length);

The program stalls, until new text data is sent from the client. If I call

namedPipeReader.Read();

I can read the binary data I expect, although it is incorrect (I would guess due to StreamReader trying to treat them as characters.)

Why is this happening? What do I need to do to read both text and binary data from a NamePipeServerStream?

sebf
  • 2,831
  • 5
  • 32
  • 50
  • This is not per se a problem with NamedPipeServerStream. The issue is mixing text and binary. The StreamReader is going to use an internal buffer so at least some of the binary data is going to end up in the buffer. You are probably going to have to read directly from the stream without StreamReader and use the proper encoding's Decoder (via GetDecoder) to pull out what ever text you want and preserve the binary data. – Mike Zboray Dec 30 '16 at 03:42
  • @Mike, Thanks, if you put that as an answer I'll accept it. Do you have links to any documentation which covers this in more detail? The docs from MS are quite lacking. The existence of a BaseStream member to me implies that it should be safe to use unless stated otherwise, but the only reference I can find to the fact that StreamReader has an internal buffer is in this Remarks section: https://msdn.microsoft.com/en-us/library/system.io.streamreader.basestream(v=vs.110).aspx ...and its not exactly what I would call clear! – sebf Dec 30 '16 at 12:22

0 Answers0