2021-08-31

基于命名管道的全双工通讯实现

关键词:NamedPipeStream

   简述:基于控制台应用程序,使用两个管道实现了全双工的进程间通信。其中,客户端和服务器端的管道读线程是新建异步线程实现的,写管道是主线程。本人菜鸟,抛砖引玉,希望能看到单管道实现的全双工通信。

#客户端的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.IO.Pipes;

namespace Named_pipe_client
{
    class Program
    {
        static void Main(string[] args)
        {              
            Thread thd = new Thread(ReadMessage);
            thd.Start();
            WriteMessage();
            System.Environment.Exit(0);
        }
        private static void ReadMessage(object obj)
        {
            using (var server = new NamedPipeClientStream(".", "Pipe_Write", PipeDirection.InOut))
            {
                Console.WriteLine("[Client]:Attemping to connect to Pipe_Write...");
                server.Connect();
                Console.WriteLine("[Client]:Connected to Pipe_Write");
                StreamReader reader = new StreamReader(server);
                string message;
                while (true)
                {
                    message = reader.ReadLine();
                    if (message != null)
                    {
                        Console.WriteLine("[Server]: " + message);
                    }
                    Thread.Sleep(100);
                }
            }
        }
        private static void WriteMessage()
        {
            using (var server = new NamedPipeClientStream(".", "Pipe_Read", PipeDirection.InOut))
            {
                Console.WriteLine("[Client]:waiting client to connect Pipe_Read...");
                server.Connect();
                Console.WriteLine("[Client]:Connected to Pipe_Read");
                StreamWriter writer = new StreamWriter(server);
                writer.AutoFlush = true;

                while (true)
                {
                    string input = Console.ReadLine();
                    if (input.ToUpper() == "EXIT")
                    {
                        Console.WriteLine("[Client]: Exiting in 3 second");
                        try
                        {
                            writer.WriteLine("Client is closing...");
                            server.WaitForPipeDrain();
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Sever not connected."); ;
                        }
                        Thread.Sleep(3000);
                        break;
                    }
                    writer.WriteLine(input);
                    server.WaitForPipeDrain();
                    Thread.Sleep(200);
                }
            }
            return;          
        }
    }
}

服务器端的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

namespace NamePipe
{
    public class PipeServer
    {
        public static PipeServer Instance {
            get
            {
                if (instance==null)
                {
                    lock (obj)
                    {
                        if (instance==null)
                        {
                            instance = new PipeServer();
                        }
                    }
                }
                return instance;
            }
        }
        static void Main(string[] args)
        {
            Thread thd = new Thread(ReadMessage);
            thd.Start();
            WriteMessage();
            System.Environment.Exit(0);
        }
        private static void ReadMessage()
        {
            using (var server = new NamedPipeServerStream("Pipe_Read", PipeDirection.InOut))
            {
                Console.WriteLine("[Server]:Pipe_Read waiting client to connect...");
                server.WaitForConnection();
                Console.WriteLine("[Server]:Pipe_Read connected");

                StreamReader reader = new StreamReader(server);
                string message;
                while (true)
                {
                    message = reader.ReadLine();
                    if (message != null)
                    {
                        Console.WriteLine("[Client]: " + message);
                    }
                    Thread.Sleep(100);
                }
            }
        }
        private static void WriteMessage()
        {
            using (var server = new NamedPipeServerStream("Pipe_Write", PipeDirection.InOut))
            {
                Console.WriteLine("[Server]:Pipe_Write waiting client to connect...");
                server.WaitForConnection();
                Console.WriteLine("[Server]:Pipe_Write connected");
                StreamWriter writer = new StreamWriter(server);
                writer.AutoFlush = true;
                while (true)
                {
                    string input = Console.ReadLine();
                    if (input.ToUpper() == "EXIT")
                    {
                        Console.WriteLine("[Server]: Exiting in 3 second");
                        try
                        {
                            writer.WriteLine("Sever is closing...");
                            server.WaitForPipeDrain();
                        }
                        catch (Exception)
                        {

                            Console.WriteLine("Client not connected."); ;
                        }
                        Thread.Sleep(3000);
                        break;
                    }
                    writer.WriteLine(input);
                    server.WaitForPipeDrain();
                    Thread.Sleep(200);
                }
                return;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值