Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named pipes usage. Multiple clients, one server, multiple parallel requests

Tags:

I'm trying to implement a named pipes server in .NET. The client will be C++. The nature of the data sent is not relevant to the question.

My first naive implementation looks something like:

using (NamedPipeServerStream stream = 
                new NamedPipeServerStream(PipeName,
                                          PipeDirection.InOut, 
                                          numberOfListeners,
                                          PipeTransmissionMode.Message))
{
     while (true)
     {
          try
          {
              stream.WaitForConnection();

              var request = ReadRequest(stream);

              var reply = Process(request);
              WriteReply(stream, reply);
              stream.WaitForPipeDrain();
          }
          catch (Exception ex)
          {
              //TO DO: log
          }
     }
 }

Am I approaching this right?

What would happen when two clients open a connection at the same time ?

Will they share the same stream and data will be intermingled ?

How can I avoid this?

Any ideas or resources on this will help. I'm pretty new to this topic.

like image 881
Adrian Zanescu Avatar asked Apr 07 '11 16:04

Adrian Zanescu


People also ask

What is a pipe instance?

The simplest pipe server creates a single instance of a pipe, connects to a single client, communicates with the client, disconnects from the client, closes the pipe handle, and terminates. However, it is more common for a pipe server to communicate with multiple pipe clients.

Can multiple processes write to the same named pipe?

Yes, multiple processes can read from (or write to) a pipe. But data isn't duplicated for the processes.

What is a named pipe C#?

A Named Pipe is one-way or duplex pipe for communication between a pipe server and one or more pipe clients. All instances of a Named Pipe share the same pipe name but each instance has its own buffers and handles. Named Pipes provide shared memory for inter-process communication.

What are named pipes used for?

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.


1 Answers

You will want the server to be able to handle concurrent client connections, and each client to connect to the server on a different instance of the pipe, rather than trying to do everything on one pipe instance as your code does at present. When the client is finished, you want to release the instance of the pipe used when talking to that client, not reuse it.

This answer gives a pseudo-code outline of one way to do this.

Also note that in Message mode you need to read from the pipe in a loop, until the IsMessageComplete property becomes true. It's the only way to guarantee you get each complete message. Also be aware that "message" in message mode means a stream of bytes written by the sender in one Write call to the pipe.

Client streams can't get mixed up with each other: a pipe instance has just two ends, THE client and THE server.

like image 138
Chris Dickson Avatar answered Oct 12 '22 15:10

Chris Dickson