Simple Example of Using Pipes with C#

So I decided to dig into understanding how Asp.net works under the hood, out of curiosity.  In the articles that I found the authors described the architecture in lower level details.  One of the things that caught my eyes was the use of named Pipes as a means of inter-process communications by the architecture.   So I decided to dig into that portion a little bit deeper.  I discovered that the .Net framework has a namespace for named pipes and it's quite easy to use. 

I intend to demonstrate a small example that I cooked up rather quickly by setting up a named pipe server and then connecting a pipe client to it.  Before I do that, I want to say as a disclaimer that I don't know that this example demonstrates the best way to use pipes.  What it does do is show the most straightforward way to use them.  Also, before you use pipes you should consider using something else like WCF instead if you need well defined interfaces, as using pipes is pretty raw.   Being the type of person that I am I like working with the "raw" components.

Without getting into too many details I can describe the concept by saying that I created two console applications in Visual Studio 2010.  One application to assume the role of the client and the other to assume the role of the server.  Each application is pretty straightforward.  The client just waits in a loop and when it gets a connection from the client it echos the data from the client onto the console.  The client on the other hand prompts for input and when it gets it it simply writes it to the server.  Below are the screenshots of console.

As an added bonus I added a key word ("close") to demonstrate the use of a command to the server.  Whenever the server receives the CLOSE command it breaks out of the loop and the application closes.  It is not the most elegant command but and example nonetheless.  Remember this is pretty raw and if you want to do anything significant with it your would need to create a messaging protocol of some sort.


Here is the code for each console app.


using System;
using System.Text;
using System.IO.Pipes;

namespace PipeClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //This is the client code that connects to a server pipe named mypipe
            //It will attempt to connect after you enter something
            //Then it will send the string up to the server pipe via the Write method
            while(true)
            {
                //Create Client to connect to server Pipe named: mypipe
                System.IO.Pipes.NamedPipeClientStream clientStream = new NamedPipeClientStream("mypipe");
                clientStream.Connect(60);

                Console.WriteLine("Enter something: ");

                string line = Console.ReadLine();

                if (line.ToLower() == "quit")
                    break;

                byte[] buffer = ASCIIEncoding.ASCII.GetBytes(line);

                clientStream.Write(buffer, 0, buffer.Length);

                clientStream.Close();
            }
        }
    }
}


using System;
using System.Text;
using System.IO.Pipes;

namespace PipeSample
{
    class Program
    {
        static void Main(string[] args)
        {
            //This is a server pipe named mypipe
            //The only thing it does is echo whatever it has received to the console
            Console.WriteLine("Waiting for connection on named pipe mypipe");
           
            while (true)
            {
                System.IO.Pipes.NamedPipeServerStream namedPipeServerStream = new NamedPipeServerStream("mypipe");

                namedPipeServerStream.WaitForConnection();

                byte[] buffer = new byte[255];

                namedPipeServerStream.Read(buffer, 0, 255);

                string request = ASCIIEncoding.ASCII.GetString(buffer);

                Console.WriteLine(request);

                request = request.Trim('\0');

                namedPipeServerStream.Close();

                if (request.ToLower() == "close")
                    break;
               
            }
        }
    }
}

Enjoy!





Comments

Mike M. said…
psh, get raw with some c sockets :)
Zipwax said…
try writing an example that doesn't open/write/close immediately. a useful example would Ping-Pong several messages back and forth between client & server.

Popular posts from this blog

Difference Between Adapter and Bridge Design Patterns

Remote Access to Your Home Desktop Using No-IP