命名管道的示例

Jor*_*nor 124 c# ipc named-pipes

我如何编写一个简单的 - 最简单的工作 - 测试应用程序,说明如何使用IPC /命名管道?

例如,如何编写一个控制台应用程序,程序1将"Hello World"写入程序2,程序2接收消息并将"Roger That"回复给程序1.

L.B*_*L.B 155

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();
                }
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果你使用的是.NET 4.5,你可以[用`Task.Run`替换`Task.Factory.StartNew`](http://stackoverflow.com/a/22087211/1185136). (11认同)
  • @Gusdor我本可以使用一些同步-prepiteves.但是阅读起来会更难.我认为这足以让我们了解如何使用NamedPipes (2认同)
  • 如果您有一个读取后管道关闭的问题,请检查以下答案:http://stackoverflow.com/a/895656/941764 (2认同)
  • 你必须处理`reader` /`writer`?如果是这样,你只处理其中一个吗?我从未见过两个都连接到同一个流的示例. (2认同)

Mar*_*nen 20

对于不熟悉IPC和命名管道的人,我发现以下NuGet包是一个很好的帮助.

GitHub:.NET 4.0的命名管道包装器

要先使用,请先安装包:

PS> Install-Package NamedPipeWrapper
Run Code Online (Sandbox Code Playgroud)

然后是一个示例服务器(从链接复制):

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();
Run Code Online (Sandbox Code Playgroud)

示例客户端:

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

client.Start();
Run Code Online (Sandbox Code Playgroud)

对我来说最好的事情是,与此处接受的答案不同,它支持多个客户端与单个服务器通信.

  • 我不推荐这个NuGet包用于生产.我已经实现了它并且它有一些错误,主要是因为无法真正知道消息何时在管道的另一端被完全接收(导致连接断开或连接结束太快(检查代码) github,如果你不信任我,"WaitForPipeDrain"在它应该的时候没有被调用),而且你将拥有多个客户端,即使只有一个人在监听,因为......太多问题).这很难过,因为它非常容易使用.我不得不从头开始重建一个选项. (4认同)
  • @MartinLaukkanen:您好,我打算使用 NamedPipeWrapper,您知道哪个 fork 正在修复此错误吗?谢谢 (2认同)

Mic*_*hip 13

您实际上可以使用其名称btw写入命名管道.

以管理员身份打开命令shell以绕过默认的"访问被拒绝"错误:

echo Hello > \\.\pipe\PipeName
Run Code Online (Sandbox Code Playgroud)


ast*_*ght 5

Linux dotnet core 不支持命名管道!

如果您部署到 Linux,请尝试 TcpListener

此 NamedPipe 客户端/服务器代码将一个字节往返到服务器。

  • 客户端写入字节
  • 服务器读取字节
  • 服务器写入字节
  • 客户端读取字节

DotNet Core 2.0 服务器控制台应用程序

using System;
using System.IO.Pipes;
using System.Threading.Tasks;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            var server = new NamedPipeServerStream("A", PipeDirection.InOut);
            server.WaitForConnection();

            for (int i =0; i < 10000; i++)
            {
                var b = new byte[1];
                server.Read(b, 0, 1); 
                Console.WriteLine("Read Byte:" + b[0]);
                server.Write(b, 0, 1);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

DotNet Core 2.0 客户端控制台应用程序

using System;
using System.IO.Pipes;
using System.Threading.Tasks;

namespace Client
{
    class Program
    {
        public static int threadcounter = 1;
        public static NamedPipeClientStream client;

        static void Main(string[] args)
        {
            client = new NamedPipeClientStream(".", "A", PipeDirection.InOut, PipeOptions.Asynchronous);
            client.Connect();

            var t1 = new System.Threading.Thread(StartSend);
            var t2 = new System.Threading.Thread(StartSend);

            t1.Start();
            t2.Start(); 
        }

        public static void StartSend()
        {
            int thisThread = threadcounter;
            threadcounter++;

            StartReadingAsync(client);

            for (int i = 0; i < 10000; i++)
            {
                var buf = new byte[1];
                buf[0] = (byte)i;
                client.WriteAsync(buf, 0, 1);

                Console.WriteLine($@"Thread{thisThread} Wrote: {buf[0]}");
            }
        }

        public static async Task StartReadingAsync(NamedPipeClientStream pipe)
        {
            var bufferLength = 1; 
            byte[] pBuffer = new byte[bufferLength];

            await pipe.ReadAsync(pBuffer, 0, bufferLength).ContinueWith(async c =>
            {
                Console.WriteLine($@"read data {pBuffer[0]}");
                await StartReadingAsync(pipe); // read the next data <-- 
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)