How to use named pipes in C# correctly -- several connections, server recreation etc
Asked Answered
H

2

18

I need to implement an inter-process communication between C# applications. I decided to use named pipes and wrote the following code:

Server

while (true)
{
    using (var server = new NamedPipeServerStream("some_pipe"))
    {
        server.WaitForConnection();
        using (var reader = new StreamReader(server))
        {
            string line = reader.ReadLine();
            MessageBox.Show(line);
        }
    }
}

Client

using (var client = new NamedPipeClientStream("some_pipe"))
{
    client.Connect();

    using (var writer = new StreamWriter(client))
    {
        writer.AutoFlush = true;
        writer.WriteLine(path);
        client.WaitForPipeDrain();
    }
}

I have the following questions about it:

  • Is this solution correct at all? Is there any issues or bugs you already see?
  • What will happen if the second client will try to connect to this server? Will it wait for the first client to finish or just send its message along with another client's instance resulting in garbage?
  • Should I really create another instance of NamedPipeServerStream class every time? If I move its construction from the while (true) loop it gives me an exception "Cannot access a closed pipe". How can I avoid it? If I can't avoid it and should re-create this object every time, what will happen if there is no NamedPipeServerStream at the moment when client will try to connect to it?
Hendeca answered 24/8, 2015 at 8:47 Comment(3)
I'd put the server pipe in it's own AppDomain. Network clients can prevent the server from re-creating the pipe if they are still using it. Unloading the domain and the pipe gets around thatClapp
@Micky Duncan Can you give me an example pls?Hendeca
I tried the same thing...It seemed that the client is closed when the StreamWriter is disposed.Athanasius
S
12

This will not work. Multiple clients connecting to just one NamedPipeServerStream is a feature not available. Its name with "...ServerStream" is misleading. Pipes are 1 to 1 connections. You must have multiple server instances serving the same pipe for many clients. This Code Project shows how to implement a Server Pipes and may help you.

Simson answered 21/8, 2016 at 2:45 Comment(3)
But here on MSDN it says "Named pipes can be one-way or duplex. They support message-based communication and allow multiple clients to connect simultaneously to the server process using the same pipe name".Kaput
@ensisNoctis To the same server process, not the same NamedPipeServerStream. The server process needs to start as many NamedPipeServerStreams as there are clients.Offing
Multiple streams is not the same as the answer suggestion to start multiple server instances (ie apps) which makes it rubbishMaury
E
1

Here is a sample that creates a named pipe server that listens to n client connections concurrently.

It essentially listens for one incoming client connection at a time, and as soon as the connection is made, it starts listening for the next one (using a new NamedPipeServerStream object) while still exchanging messages with the prior one(s).

Economy answered 8/4 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.