Thursday, April 12, 2007 4:52 AM bart

Getting started with named pipes

While preparing a little "Orcas" demo, I ended up with the System.IO.Pipes namespace that provides support for named pipes. In the past, I've been doing some named pipe work using unmanaged code and using interop, but in Orcas it becomes as simple as any other kind of I/O (if you know some basic concepts of named pipes though).

Below is a very little sample that shows how easy it is to use named pipes; it's part of a bigger demo that shows how to use pipes for (secure, using access control which is supported on named pipes too) communication between a service agent (in the taskbar notification area) and a Windows Service running in the background, even with cross-machine support. Here's the code of the "reduced" demo:

Named pipes for dummies - Copy Code
1 using System; 2 using System.IO; 3 using System.IO.Pipes; 4 using System.Threading; 5 6 class Program 7 { 8 static string NAME = "Demo"; 9 static ManualResetEvent evt = new ManualResetEvent(false); 10 11 static void Server() 12 { 13 using (NamedPipeServerStream server = new NamedPipeServerStream(NAME, PipeDirection.InOut)) 14 { 15 evt.Set(); 16 WriteGreen("Waiting for connection ..."); 17 server.WaitForConnection(); 18 WriteGreen("Connection established."); 19 20 using (StreamReader sr = new StreamReader(server)) 21 { 22 using (StreamWriter sw = new StreamWriter(server)) 23 { 24 int i = 0; 25 while (true) 26 { 27 i = int.Parse(sr.ReadLine()); 28 string s = Convert.ToString(i, 2); 29 WriteGreen("SERVER: Received {0}, sent {1}.", i, s); 30 sw.WriteLine(s); 31 sw.Flush(); 32 Thread.Sleep(1000); 33 } 34 } 35 } 36 } 37 } 38 39 static void Main(string[] args) 40 { 41 new Thread(Server).Start(); 42 evt.WaitOne(); 43 44 using (NamedPipeClientStream client = new NamedPipeClientStream(".", NAME, PipeDirection.InOut)) 45 { 46 WriteRed("Connecting to server ..."); 47 client.Connect(); 48 WriteRed("Connected."); 49 50 using (StreamWriter sw = new StreamWriter(client)) 51 { 52 using (StreamReader sr = new StreamReader(client)) 53 { 54 Random rand = new Random(); 55 56 while (true) 57 { 58 int i = rand.Next(1000); 59 sw.WriteLine(i); 60 sw.Flush(); 61 string s = sr.ReadLine(); 62 WriteRed("CLIENT: Sent {0}, received {1}.", i, s); 63 } 64 } 65 } 66 } 67 } 68 69 static void WriteRed(string msg, params object[] p) 70 { 71 Console.ForegroundColor = ConsoleColor.Red; 72 Console.WriteLine(msg, p); 73 Console.ResetColor(); 74 } 75 76 static void WriteGreen(string msg, params object[] p) 77 { 78 Console.ForegroundColor = ConsoleColor.Green; 79 Console.WriteLine(msg, p); 80 Console.ResetColor(); 81 } 82 }

And here's the output:

Another reason to take a closer look at Orcas... Beta coming soon!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

# re: Getting started with named pipes

Thursday, April 12, 2007 8:23 PM by dotnetjunkie

Interesting, but I wonder:

What are the reasons or benefits of using Named Pipes for service agent <-> service communication?

Why would you prefer this over WCF for example?

(or Sockets, or Memory Mapped Files...)

# re: Getting started with named pipes

Friday, April 13, 2007 3:43 AM by bart

Hi dotnetjunkie,

You're absolutely right about this; it's just a matter of where you are (/want to be) on the overall stack. Named pipes is a low-level communication mechanism just like raw sockets (it's a Stream after all), while things like WCF, .NET Remoting, DCOM, etc are higher-level constructs layered on top of those low-level constructs. For example, WCF can talk over named pipes (though it doesn't use System.IO.Pipes yet).

A possible use is communication with another application that already does use named pipes (and has a well-documented protocol for named pipes communication). Another case might be the need for raw performance when transferring lots of data without going up and down the "library stack" (although I haven't measured perf differences yet).

-Bart

# Named Pipes in VS 2008

Wednesday, June 27, 2007 2:22 PM by msdn Austria

Bart De Smet schreibt auf seinem Blog über den neuen NamedPipeClientStream / NamedPipeServerStream und

# .NET3.5(VS2008)から名前付きパイプ

Wednesday, June 27, 2007 7:57 PM by 中の技術日誌ブログ

.NET3.5(VS2008)から名前付きパイプ

# Running Object Table and .NET

Friday, September 30, 2011 3:27 PM by Mauricio Rojas Blog

What is the ROT? “Using ROT ( Running Object Table ) is a great way to establish interprocess com munication

# Running Object Table and .NET

Monday, January 28, 2013 10:56 AM by ArtinSoft's Blogs!

Running Object Table and .NET