NamedPipeStream的使用

NamedPipeStream的使用

NamedPipeStream的使用具体案例如下:

using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.IO.Pipes;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SupremeConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 测试  NamedPipeStream
            new Thread(new ThreadStart(PipeServer)).Start();
            new Thread(new ThreadStart(PipeClient)).Start();
            #endregion
         }
    public static void PipeServer()
        {
            var s = new NamedPipeServerStream("pipedream");
            s.WaitForConnection();
            while (true)
            {

                s.WriteByte(100);
                Console.WriteLine($"PipeServer 收到 客户端 数据:{s.ReadByte()}");
            }
        }

        public static void PipeClient()
        {

           var s = new NamedPipeClientStream("pipedream");
            s.Connect();
            while (true)
            {

                Console.WriteLine($"PipeClient 收到服务端数据:{s.ReadByte()}");
                Thread.Sleep(TimeSpan.FromSeconds(2));
                s.WriteByte(200); // Send the value 200 back.
            }
        }
     }
}

 测试结果:

01-01 06:21