Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of Named Pipes

How do I write a simple--bare minimum needed for it to work--test application that illustrates how to use IPC/Named Pipes?

For example, how would one write a console application where Program 1 says "Hello World" to Program 2 and Program 2 receives message and replies "Roger That" to Program 1.

like image 801
Jordan Trainor Avatar asked Dec 10 '12 17:12

Jordan Trainor


People also ask

How do you write in a named pipe?

Within the application program, you create a named pipe by coding a mkfifo() or mknod() function. You give the FIFO a name and an access mode when you create it. If the access mode allows all users read and write access to the named pipe, any process that knows its name can use it to send or receive data.

Where are named pipes used?

Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network. If the server service is running, all named pipes are accessible remotely.

Is FIFO A named pipe?

Another name for named pipe is FIFO (First-In-First-Out).

What are named and unnamed pipes?

An unnamed pipe is a direct connection between two commands running in the same terminal. If we want to send output from a command in one terminal to another command in a different terminal, we can use a named pipe, or FIFO. FIFO stands for first in, first out. This is a pipe that exists in the file system.


2 Answers

using System; using System.IO; using System.IO.Pipes; using System.Linq; using System.Text; using System.Threading.Tasks;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             StartServer();             Task.Delay(1000).Wait();               //Client             var client = new NamedPipeClientStream("PipesOfPiece");             client.Connect();             StreamReader reader = new StreamReader(client);             StreamWriter writer = new StreamWriter(client);              while (true)             {                 string input = Console.ReadLine();                 if (String.IsNullOrEmpty(input)) break;                 writer.WriteLine(input);                 writer.Flush();                 Console.WriteLine(reader.ReadLine());             }         }          static void StartServer()         {             Task.Factory.StartNew(() =>             {                 var server = new NamedPipeServerStream("PipesOfPiece");                 server.WaitForConnection();                 StreamReader reader = new StreamReader(server);                 StreamWriter writer = new StreamWriter(server);                 while (true)                 {                     var line = reader.ReadLine();                     writer.WriteLine(String.Join("", line.Reverse()));                     writer.Flush();                 }             });         }     } } 
like image 165
L.B Avatar answered Oct 12 '22 02:10

L.B


For someone who is new to IPC and Named Pipes, I found the following NuGet package to be a great help.

GitHub: Named Pipe Wrapper for .NET 4.0

To use first install the package:

PS> Install-Package NamedPipeWrapper 

Then an example server (copied from the link):

var server = new NamedPipeServer<SomeClass>("MyServerPipe"); server.ClientConnected += delegate(NamedPipeConnection<SomeClass> conn)     {         Console.WriteLine("Client {0} is now connected!", conn.Id);         conn.PushMessage(new SomeClass { Text: "Welcome!" });     };  server.ClientMessage += delegate(NamedPipeConnection<SomeClass> conn, SomeClass message)     {         Console.WriteLine("Client {0} says: {1}", conn.Id, message.Text);     };  server.Start(); 

Example client:

var client = new NamedPipeClient<SomeClass>("MyServerPipe"); client.ServerMessage += delegate(NamedPipeConnection<SomeClass> conn, SomeClass message)     {         Console.WriteLine("Server says: {0}", message.Text);     };  client.Start(); 

Best thing about it for me is that unlike the accepted answer here it supports multiple clients talking to a single server.

like image 36
Martin Laukkanen Avatar answered Oct 12 '22 04:10

Martin Laukkanen