zoukankan      html  css  js  c++  java
  • AnonymousPipeStream的使用案例

    AnonymousPipeStream的使用具体案例如下:

    服务端:

    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(AnonymousPipeServer)).Start();
                #endregion
             } 
    
            public static void AnonymousPipeServer()
            {
                string clientExe = @"F:PersonLongtengLongtengSlnConsoleAppTestAnonymousPipeinDebugConsoleAppTestAnonymousPipe.exe";
                HandleInheritability inherit = HandleInheritability.Inheritable;
                using (var tx = new AnonymousPipeServerStream(PipeDirection.Out, inherit))
                using (var rx = new AnonymousPipeServerStream(PipeDirection.In, inherit))
                {
                    txID = tx.GetClientHandleAsString();
                    rxID = rx.GetClientHandleAsString();
                    var startInfo = new ProcessStartInfo(clientExe, txID + " " + rxID);
                    startInfo.UseShellExecute = false; // Required for child process
                    Process p = Process.Start(startInfo);
                    tx.DisposeLocalCopyOfClientHandle(); // Release unmanaged
                    rx.DisposeLocalCopyOfClientHandle(); // handle resources.
                    //tx.WriteByte(100);
                    //Console.WriteLine("Server received: " + rx.ReadByte());
                    //p.WaitForExit();    
                    while (true)
                    {
                        tx.WriteByte(100);
                        Console.WriteLine("Server received: " + rx.ReadByte());
                    }
                               
                }
            }
         }
    }

    客户端(新建一个控制台程序):

    using System;
    using System.IO.Pipes;
    using System.Threading;
    
    namespace ConsoleAppTestAnonymousPipe
    {
        class Program
        {
            static void Main(string[] args)
            {
                string rxID = args[0]; // Note we're reversing the
                string txID = args[1]; // receive and transmit roles.
                using (var rx = new AnonymousPipeClientStream(PipeDirection.In, rxID))
                using (var tx = new AnonymousPipeClientStream(PipeDirection.Out, txID))
                {
                    //Console.WriteLine("Client received: " + rx.ReadByte());
                    //tx.WriteByte(200);
                    while (true)
                    {
                        Console.WriteLine("Client received: " + rx.ReadByte());
                        tx.WriteByte(200);
                        Thread.Sleep(TimeSpan.FromSeconds(1));
                    }
                }
            }
        }
    }

    运行结果:

  • 相关阅读:
    VirtualBox中的网络连接方式详解
    DRUID连接池的实用 配置详解
    redis之如何配置jedisPool参数
    怎么把myeclipse项目导入IDEA中
    最新Hadoop大数据开发学习路线图
    编程能力七段论(下)
    编程能力七段论(上)
    移动无线测试技能树
    WebView加载网页不显示图片解决办法
    编程能力七段论
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/11911299.html
Copyright © 2011-2022 走看看