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.

Advertisements

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

3 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?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: