• C#进程间通信-匿名管道通信


    这里是官方文档,已经很完善了,Server拉Client,Server发Client收。
    我根据自己的需要进行了一些修改:

    1. 要由被拉起的子进程来发送,父进程接收;
    2. 支持持续发送和接收消息;
    3. 对上述操作进行类的封装。
      代码如下:
    Client端代码
    using System.IO.Pipes;
    
    namespace TestPipe
    {
        class AnonymousPipeClient
        {
            private PipeStream pipeClient;
            private StreamWriter sw;
            public AnonymousPipeClient(PipeDirection direction, string pipeHandleAsString)
            {
                try
                {
                    pipeClient = new AnonymousPipeClientStream(direction, pipeHandleAsString);
                    sw = new StreamWriter(pipeClient);
                    sw.AutoFlush = true;
                    Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.", pipeClient.TransmissionMode);
                }
                catch (Exception e)
                {
                    Console.WriteLine("[CLIENT] Construct error: {0}", e.Message);
                }
            }
    
            public void Send()
            {
                try
                {
                    // Send a 'sync message' and wait for client to receive it.
                    sw.WriteLine("SYNC");
                    sw.Flush();
                    pipeClient.WaitForPipeDrain();
                    // Send the console input to the client process.
                    Console.Write("[CLIENT] Enter text: ");
                    sw.WriteLine(Console.ReadLine());
                }
                catch (IOException e)
                {
                    Console.WriteLine("[CLIENT] Send error: {0}", e.Message);
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                AnonymousPipeClient client = new AnonymousPipeClient(PipeDirection.Out, args[0]);
                while (true)
                {
                    client.Send();
                }
                
                Console.Write("[CLIENT] Press Enter to continue...");
                Console.ReadLine();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    Server端代码
    using System.Diagnostics;
    using System.IO.Pipes;
    
    namespace TestPipe
    {
        class AnonymousPipeServer
        {
            public AnonymousPipeServerStream PipeServer { get; private set; }
            private StreamReader sr;
            public AnonymousPipeServer(PipeDirection direction)
            {
                try
                {
                    PipeServer = new AnonymousPipeServerStream(direction, HandleInheritability.Inheritable);
                    sr = new StreamReader(PipeServer);
                    Console.WriteLine("[SERVER] Current TransmissionMode: {0}.", PipeServer.TransmissionMode);
                }
                catch (Exception e)
                {
                    Console.WriteLine("[SERVER] Construct error: {0}", e.Message);
                }
            }
    
            public void Receive()
            {
                while (true)
                {
                    string temp;
                    bool printed = false;
                    // Wait for 'sync message' from the server.
                    do
                    {
                        temp = sr.ReadLine();
                        if (temp == null || temp.Equals(""))
                        {
                            if (!printed)
                            {
                                Console.WriteLine("[SERVER] Wait for sync...");
                                printed = true;
                            }
                        }
                        temp = temp != null ? temp : "";
                        Thread.Sleep(100);
                    }
                    while (!temp.StartsWith("SYNC"));
                    printed = false;
    
                    // Read the server data and echo to the console.
                    temp = sr.ReadLine();
                    if (temp != null)
                    {
                        Console.WriteLine("[SERVER] Echo: " + temp);
                    }
                    Thread.Sleep(50);
                }
            }
        }
        class Program
        {
            static void Main()
            {
                AnonymousPipeServer anonymousPipeServer = new AnonymousPipeServer(PipeDirection.In);
    
                Process pipeClient = new Process();
                pipeClient.StartInfo.FileName = "../../../../AnonymousPipeClient/bin/Debug/net6.0/AnonymousPipeClient.exe";
                // Pass the client process a handle to the server.
                pipeClient.StartInfo.Arguments = anonymousPipeServer.PipeServer.GetClientHandleAsString();
                pipeClient.StartInfo.UseShellExecute = false;
                pipeClient.Start();
    
                anonymousPipeServer.PipeServer.DisposeLocalCopyOfClientHandle();
                anonymousPipeServer.Receive();
    
                pipeClient.WaitForExit();
                pipeClient.Close();
                Console.WriteLine("[SERVER] Client quit. Server terminating.");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
  • 相关阅读:
    UI for Apache Kafka
    【NLP】情绪分析与酒店评论
    SpringBoot 拦截器的使用
    7.继承与多态 对象村的优质生活
    CardView设置任意角为圆角
    设计原则之-单一职责原则
    AIGC领航,智能AI赋能乡村教育,梦想扬帆远航
    如何把Android Framework学彻底?一条龙学习
    未来各职业的人,都会涌入Python和AI大潮中,老教授深度解析
    Docker部署Emqx并配置ssl支持微信小程序
  • 原文地址:https://blog.csdn.net/fm0517/article/details/126604305