【C# 】Pipe管道通信使用

管道通信

        管道通信(Pipe Communication)可以用来在两个或多个进程之间传递数据。

        管道可以是匿名的也可以是有名的,有名管道允许不同进程间的通信,而匿名管道通常用于父子进程之间的通信。

        详细参考pipe管道通信原理_核间通信pipe通信-CSDN博客

 

管道实例

服务器端会创建一个命名管道并等待客户端连接,

客户端则会尝试连接到这个管道,并发送一条消息给服务器端,服务器端接收到消息后会打印出来。

服务器端代码 (PipeServer)

服务器端的代码可以写成方法,或是集成到需要的地方使用。参考代码如下:

using System;
using System.IO.Pipes;
using System.Text;

class PipeServer
{
    static void Main(string[] args)
    {
        using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("MyPipeName", PipeDirection.InOut))
        {
            Console.WriteLine("Waiting for a client...");
            pipeServer.WaitForConnection(); // 等待客户端连接
            Console.WriteLine("Connected.");

            // 读取客户端发送的数据
            byte[] bytes = new byte[1024];
            int readBytes = pipeServer.Read(bytes, 0, bytes.Length);
            string data = Encoding.ASCII.GetString(bytes, 0, readBytes);
            Console.WriteLine($"Received: {data}");

            // 向客户端发送响应
            string response = "Hello back from the server!";
            byte[] responseBytes = Encoding.ASCII.GetBytes(response);
            pipeServer.Write(responseBytes, 0, responseBytes.Length);
            pipeServer.Flush();

            pipeServer.Disconnect(); // 断开连接
        }
    }
}

客户端代码 (PipeClient)

客户端的代码可以写成方法,或是集成到需要的地方使用。参考代码如下: 

using System;
using System.IO.Pipes;
using System.Text;

class PipeClient
{
    static void Main(string[] args)
    {
        using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "MyPipeName", PipeDirection.InOut))
        {
            pipeClient.Connect(1000); // 尝试连接,超时时间为1秒
            Console.WriteLine("Connected to server.");

            // 向服务器发送数据
            string message = "Hello from the client!";
            byte[] bytes = Encoding.ASCII.GetBytes(message);
            pipeClient.Write(bytes, 0, bytes.Length);
            pipeClient.Flush();

            // 读取服务器响应
            byte[] responseBytes = new byte[1024];
            int bytesRead = pipeClient.Read(responseBytes, 0, responseBytes.Length);
            string response = Encoding.ASCII.GetString(responseBytes, 0, bytesRead);
            Console.WriteLine($"Received from server: {response}");

            pipeClient.Close(); // 关闭管道
        }
    }
}

 在这个例子中,服务器和客户端都使用相同的管道名字 "MyPipeName" 进行通信。服务器首先创建管道并等待客户端连接,而客户端则尝试连接到服务器端创建的管道,并发送一个字符串消息。服务器和客户端都实现了读写操作,使得数据可以在两者之间双向流动。当客户端向服务器发送消息后,服务器会接收到消息并回发一条响应给客户端。同样地,客户端在发送消息后也会读取来自服务器的响应。

 

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中的双工管道通信可以通过使用命名管道实现,命名管道是一种IPC方式,可以用于进程间通信。以下是一个简单的示例代码: ```csharp using System; using System.IO; using System.IO.Pipes; using System.Text; class Program { static void Main(string[] args) { // 创建命名管道服务器 var server = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 1); Console.WriteLine("等待客户端连接..."); server.WaitForConnection(); Console.WriteLine("客户端已连接。"); // 读取客户端发送的消息 var reader = new StreamReader(server); var message = reader.ReadLine(); Console.WriteLine($"客户端: {message}"); // 发送响应消息给客户端 var writer = new StreamWriter(server); writer.WriteLine("Hello, client!"); writer.Flush(); // 关闭管道 server.Close(); } } ``` 客户端代码: ```csharp using System; using System.IO; using System.IO.Pipes; using System.Text; class Program { static void Main(string[] args) { // 连接到命名管道服务器 var client = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut); Console.WriteLine("连接到服务器..."); client.Connect(); Console.WriteLine("已连接到服务器。"); // 发送消息给服务器 var writer = new StreamWriter(client); writer.WriteLine("Hello, server!"); writer.Flush(); // 读取服务器响应消息 var reader = new StreamReader(client); var message = reader.ReadLine(); Console.WriteLine($"服务器: {message}"); // 关闭管道 client.Close(); } } ``` 以上代码实现了一个简单的双工管道通信,当客户端连接到命名管道服务器后,客户端和服务器可以相互发送消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangnaisheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值