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.

Advertisement

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

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

  1. Josip Štajdohar says:

    Could you pleas give me advice on how to send whole arrays of byte data at once using a named pipe. Do i need to create an object like in the fifth part or is there a simpler way? I am used to pipes in c++ and there you can send any data type as long as the write buffer and the read buffer are declared as the specific type.

  2. Any chance of a reply to Josip Štajdohar question?

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 )

Connecting to %s

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.

%d bloggers like this: