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.
            }
        }
     }
}

 测试结果:

 

posted @ 2019-11-22 11:10  龙骑科技  阅读(647)  评论(0编辑  收藏  举报