Introducing Pipes [Justin Van Patten]

Introducing Pipes [Justin Van Patten]

Rate This
  • Comments 14

The Orcas October Community Technology Preview (CTP) includes new types that make it easy for developers to use pipes from managed code.  Pipes are used for inter-process communication (IPC) between processes running on the same machine, or processes running on any other Windows machine within a network.  We've added support for both anonymous and named pipes.

The new pipe types can be found in System.Core.dll within the System.IO namespace.  Note that after the October CTP went out, we moved the pipe types from System.IO to System.IO.Pipes, so the types will be in the new namespace in all future CTPs.

Anonymous Pipes

Anonymous pipes are character-based and are half-duplex.  They cannot communicate over the network and support only a single server instance.  These pipes are most useful for communication between threads or between parent and child processes where the pipe handles can be easily passed when the child process is created.

Example 1: Anonymous Pipes

The following example demonstrates sending a string from a parent process to a child process.  A line is read from the console of the parent and sent to the child process.  The child process then writes the string that it received from the parent process to the console.

Parent Process

Process process = new Process();

process.StartInfo.FileName = "child.exe";

 

using (AnonymousPipeServerStream pipeStream =

    new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) {

 

    process.StartInfo.Arguments = pipeStream.GetClientHandleAsString();

    process.StartInfo.UseShellExecute = false;

    process.Start();

 

    pipeStream.DisposeLocalCopyOfClientHandle();

 

    using (StreamWriter sw = new StreamWriter(pipeStream)) {

        sw.AutoFlush = true;

        sw.WriteLine(Console.ReadLine());

    }

}

 

process.WaitForExit();

process.Close();


Child Process

using (StreamReader sr = new StreamReader(

    new AnonymousPipeClientStream(PipeDirection.In, args[0]))) {

    string line;

    while ((line = sr.ReadLine()) != null) {

        Console.WriteLine("Echo: {0}", line);

    }

}

Example output:

Anonymous pipes are sweet!

Echo: Anonymous pipes are sweet!

Named Pipes

Named pipes are far more powerful than anonymous pipes.  They can be duplex, over the network, and can support multiple server instances of a single name, making them ideal for quick-to-build, and easy to connect to, multithreaded servers.  Furthermore, they support message based communication so that a reading process can read varying-length messages precisely as sent by the writing process.  Finally, named pipes support impersonation allowing connecting processes to use their own set of permissions on remote servers.

Example 2: Named Pipes

In cases where the second process cannot inherit the pipe handle, named pipes can be used.  The following example demonstrates sending strings from the client process to the server process.

Server Process

using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("testpipe")) {

    pipeStream.WaitForConnection();

 

    using (StreamReader sr = new StreamReader(pipeStream)) {

        string temp;

        while ((temp = sr.ReadLine()) != null) {

            Console.WriteLine("{0}: {1}", DateTime.Now, temp);

        }

    }

}


Client Process

using (NamedPipeClientStream pipeStream = new NamedPipeClientStream("testpipe")) {

    pipeStream.Connect();

 

    using (StreamWriter sw = new StreamWriter(pipeStream)) {

        sw.AutoFlush = true;

        string temp;

        while ((temp = Console.ReadLine()) != null) {

            sw.WriteLine(temp);

        }

    }

}

Example 3: Named Pipes with Messaging

Named pipes also support message-based communication.  This allows a reading process to read varying-length messages precisely as sent by the writing process.  The following example displays how such messages are sent and read.

Process 1

UTF8Encoding encoding = new UTF8Encoding();

 

string message1 = "Named Pipe Message Example.";

string message2 = "Another Named Pipe Message Example.";

Byte[] bytes;

 

using (NamedPipeServerStream pipeStream = new

        NamedPipeServerStream("messagepipe", PipeDirection.InOut, 1,

        PipeTransmissionMode.Message, PipeOptions.None)) {

    pipeStream.WaitForConnection();

 

    // Let’s send two messages.

    bytes = encoding.GetBytes(message1);

    pipeStream.Write(bytes, 0, bytes.Length);

 

    bytes = encoding.GetBytes(message2);

    pipeStream.Write(bytes, 0, bytes.Length);

}


Process 2

Decoder decoder = Encoding.UTF8.GetDecoder();

Byte[] bytes = new Byte[10];

Char[] chars = new Char[10];

 

using (NamedPipeClientStream pipeStream =

        new NamedPipeClientStream("messagepipe")) {

    pipeStream.Connect();

    pipeStream.ReadMode = PipeTransmissionMode.Message;

 

    int numBytes;

    do {

        string message = "";

        do {

            numBytes = pipeStream.Read(bytes, 0, bytes.Length);

            int numChars = decoder.GetChars(bytes, 0, numBytes, chars, 0);

            message += new String(chars, 0, numChars);

 

        } while (!pipeStream.IsMessageComplete);

 

        decoder.Reset();

 

        Console.WriteLine(message);

 

    } while (numBytes != 0);

}

The above three examples show how easy it is to achieve IPC using the new pipe types we're introducing in the Orcas release.  These types expose nearly all the pipe functionality provided by Windows.

In a future blog post we'll talk about some of the more advanced functionality that these types provide, such as multithreaded named pipes with impersonation and asynchronous IO.  In the meantime, we'd love to hear your feedback on the pipe functionality we're exposing.

  • You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous and named pipes are IPC mechanisms to communicate between processes. Basically, anonymous pipes

  • Here they are: It looks like computers have finally got it over the humans with the defeat of Vladimir...

  • Si vous étiez intéressé par le support des pipes, nommés ou anonymes, en .NET, vous avez probablement

  • Is this a component that can be used with an existing 2.0 installation? If not, is there any chance that it could be rolled out separately so that it could be used with v2.0? Thanks

  • The current plan is for this to be part of the Orcas release (i.e. .NET 3.5).  Right now there are no plans to roll this out as a separate component, but the idea is interesting.  Thanks for the feedback!

  • Great work!

    Will it be part of WCF(Windows Communication Foundation) solution? Although WCF is mainly intented for SOA developement, I am still wondering is it possible to develop high perofrmance application on that.

  • Tim Hibbard on Changing the default report interval in TFS. Sean McBreen on Visual Studio Team System...

  • Everyone used to talk about named pipes and pipes in general as the fastest way for inter-process communication

  • Jeff,

    This is not part of WCF at the moment.  Pipes are a lower level channel the WCF could be built on top of.

    WCF actually has its own internal pipe implementation that it uses to achieve IPC.  In the future, WCF may switch to using the public pipe implementation that we're now providing.

  • Mein TechTalk ist nun zu Ende. Meine letzte Station heute in Berlin war Lustig und Amüsant, ich hoffe

  • I recently wrote an MSDN Flash article on the hidden gems of System.Core.dll of .NET Framework 3.5 which

  • I recently wrote an MSDN Flash article on the hidden gems of System.Core.dll of .NET Framework 3.5 which

  • .NET Framework 3.5 and Visual Studio 2008 have officially shipped! Soma has the announcement on his blog

Page 1 of 1 (14 items)