Basics of working with pipes in C# .NET part 2: continuous byte communication

In this post we briefly introduced how interprocess communication pipes are represented in .NET. The server sent a single byte to the client and the client sent a single byte in response. We’ll add some more flesh to the code but still keep it very simple. We’ll let the client and server send each other individual bytes as messages. Obviously that is still very childish but it will do for demo purposes. We’ll continue with proper messages in the next post.

Here’s the extended server code with a lot more output. We read a line of input from the console but only transmit the very first byte. We wait for the client’s response. The byte value of ‘x’, i.e. 120 will indicate that the client wants to end the communication:

private static void SendByteAndReceiveResponseContinuous()
{
	using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("test-pipe"))
	{
		Console.WriteLine("Server waiting for a connection...");
		namedPipeServer.WaitForConnection();
		Console.Write("A client has connected, send a byte from the server: ");
		string b = Console.ReadLine();
		Console.WriteLine("About to send byte {0} to client.", b);
		namedPipeServer.WriteByte(Encoding.UTF8.GetBytes(b).First());
		Console.WriteLine("Byte sent, waiting for response from client...");
		int byteFromClient = namedPipeServer.ReadByte();
		Console.WriteLine("Received byte response from client: {0}", byteFromClient);
		while (byteFromClient != 120)
		{
			Console.Write("Send a byte response: ");
			b = Console.ReadLine();
			Console.WriteLine("About to send byte {0} to client.", b);
			namedPipeServer.WriteByte(Encoding.UTF8.GetBytes(b).First());
			Console.WriteLine("Byte sent, waiting for response from client...");
			byteFromClient = namedPipeServer.ReadByte();
			Console.WriteLine("Received byte response from client: {0}", byteFromClient);
		}
		Console.WriteLine("Server exiting, client sent an 'x'...");
	}
}

The client code is very similar. It will connect to the server and keep exchanging bytes until that byte is ‘x’:

private static void ReceiveByteAndRespondContinuous()
{
	using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream("test-pipe"))
	{
		namedPipeClient.Connect();
		Console.WriteLine("Client connected to the named pipe server. Waiting for server to send first byte...");
		Console.WriteLine("The server sent a single byte to the client: {0}", namedPipeClient.ReadByte());
		Console.Write("Provide a byte response from client: ");
		string b = Console.ReadLine();
		Console.WriteLine("About to send byte {0} to server.", b);
		namedPipeClient.WriteByte(Encoding.UTF8.GetBytes(b).First());
		while (b != "x")
		{
			Console.WriteLine("The server sent a single byte to the client: {0}", namedPipeClient.ReadByte());
			Console.Write("Provide a byte response from client: ");
			b = Console.ReadLine();
			Console.WriteLine("About to send byte {0} to server.", b);
			namedPipeClient.WriteByte(Encoding.UTF8.GetBytes(b).First());					
		}

		Console.WriteLine("Client chose to disconnect...");
	}
}

You can call both methods from Main of the server and client console applications respectively and run them both. The communication flow might resemble the following:

Named pipe server and client byte communication flow

Read the next part 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.

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: