Named Pipe Server & Client - no messages

I'm trying to learn how to make named pipes. Therefore, I created a server and client in LinqPad.

Here is my server:

var p = new NamedPipeServerStream("test3", PipeDirection.Out);
p.WaitForConnection();
Console.WriteLine("Connected!");
new StreamWriter(p).WriteLine("Hello!");
p.Flush();
p.WaitForPipeDrain();
p.Close();

Here is my client:

var p = new NamedPipeClientStream(".", "test3", PipeDirection.In);
p.Connect();
var s = new StreamReader(p).ReadLine();
Console.Write("Message: " + s);
p.Close();

I start the server and then the client, and I see "Connected!". appear on the server, so it connects correctly. However, the client always displays Message:without anything after it, so the data does not actually go from the server to the client for display. I already tried to recode the directions of the pipelines and send the client data to the server with the same result.

Why is this screen not printing data? What am I missing?

Thank!

+2
source share
2 answers

Change the server code as follows:

StreamWriter wr = new StreamWriter(p);
wr.WriteLine("Hello!\n");
wr.Flush();

StreamWriter

+2

L.B, StreamWriter. using :

using (var p = new NamedPipeServerStream("test3", PipeDirection.Out))
{
    p.WaitForConnection(); 
    Console.WriteLine("Connected!"); 
    using (var writer = new StreamWriter(p))
    {
         writer.WriteLine("Hello!");
         writer.Flush();
    }
    p.WaitForPipeDrain(); 
    p.Close();
}

, Flush() Close() , ( ). , - , - .

+3

All Articles