Basics of working with pipes in C# .NET part 1: send and receive a single byte

Pipes are used for interprocess communication. Typically there’s a single pipe server that one or more clients can connect to and exchange messages.

There are named and anonymous pipes. Anonymous pipes come with a couple of limitations compared to named pipes:

  • They are one-way only i.e. the server and client cannot exchange messages. Communication where both the server and client are allowed to send messages is called duplex
  • Anonymous pipes only allow communication with a single client. Named pipes provide a multi-client scenario
  • Anonymous pipes cannot function over the network. They are limited to the same machine. Named pipes have no such limitation

Pipes are represented by the System.IO.Pipes namespace in .NET. These are the key objects you’ll need depending on the type of pipe you’d like to work with:

  • NamedPipeServerStream and NamedPipeClientStream for named pipes
  • AnonymousPipeServerStream and AnonymousPipeClientStream for anonymous pipes

Let’s see an extremely basic named pipe server example. The pipe name is provided in the NamedPipeServerStream constructor. The server will wait for a connection and then send a single byte, a ‘1’ to the connected client. It will then wait for the client to respond with a byte and print that on the console:

private static void SendByteAndReceiveResponse()
{			
	using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("test-pipe"))
	{
		namedPipeServer.WaitForConnection();
		namedPipeServer.WriteByte(1);
		int byteFromClient = namedPipeServer.ReadByte();
		Console.WriteLine(byteFromClient);
	}
}

You can put the above code in a console application and call SendByteAndReceiveResponse from Main.

The client side of the code, which you can put in another console application, is very similar. It will connect to the client, read the byte that the server sent and then respond with another byte, a ‘2’:

private static void ReceiveByteAndRespond()
{			
	using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream("test-pipe"))
	{
		namedPipeClient.Connect();
		Console.WriteLine(namedPipeClient.ReadByte());
		namedPipeClient.WriteByte(2);
	}
}

Call this method from Main as well and start both console applications. You’ll see that the server is waiting for the client to connect and that step blocks code execution. Then when the client starts everything will go very quickly: the client console will print ‘1’ and the server console will print the client response, i.e. ‘2’.

Read the next instalment here.

View the list of posts on Messaging here.

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

6 Responses to Basics of working with pipes in C# .NET part 1: send and receive a single byte

  1. Souvik Basu says:

    Simple and Great post. I was searching for a basic code of namedpipe but just didn’t find it anywhere

  2. Thanks! Very nicely done.

  3. What if you want to use ipc between x64 process and x32 dll?

  4. Jonathan Rivera Diaz says:

    Excellent, look everywhere and it is the best info about pipes for which we are beginners in this subject. Thanks!

  5. Thaveesha Kannangara says:

    Thanks a lot man, Helps a lot. Feels like xmp-rpc 😀

  6. How do I send x number of bytes ?

Leave a comment

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.