目录结构:

contents structure [+]

管道为进程间通信提供了一种可能。管道分为两种,一种是匿名管道,另一种是命名管道。

匿名管道,匿名管道只提供在本地电脑进程间的通信。匿名管道比命名管道花费的开销更少,但提供的服务也比命名管道的少。匿名管道是单向的,而且不能用于网络通信。匿名管道只支持单服务器实例。

匿名管道不支持消息传输模式(PipeTransmissionMode.Message),仅支持字节传输模式(PipeTransmissionMode.Byte)。

创建匿名管道需要使用AnonymousPipeClientStream和AnonymousPipeServerStream类,下面的案例展示了匿名的管道的用法。
首先创建一个PipeServer.cs文件,其内容如下:

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

namespace PipeServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Process pipeClient1=new Process();
            pipeClient1.StartInfo.FileName = @"PipeClient.exe";
                        
            //AnonymousPipeServerStream is an one-way pipe. in other words it's only support PipeDirection.In or PipeDirection.Out.
            //in this program, we use PipeDirection.Out for send data to clients.
            using (AnonymousPipeServerStream pipeServer = 
                new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable)) {

                //send the client handle that from AnonymousPipeServerStream to client
                pipeClient1.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
                pipeClient1.StartInfo.UseShellExecute = false;
                pipeClient1.Start();
                
                //closes the local copy of the AnonymousPipeClientStream object's handle
                //this method must be call after the client handle has been passed to the client
                //if this method isn't called, then the AnonymousPipeServerStream will not receive notice when client dispose of it's PipeStream object
                pipeServer.DisposeLocalCopyOfClientHandle();

                try
                {
                    using(StreamWriter sw=new StreamWriter(pipeServer)){
                        //automatic flush
                        sw.AutoFlush = true;
                        
                        sw.WriteLine("SYNC");
                        //it'll block util the other end of pipe has been read all send bytes
                        pipeServer.WaitForPipeDrain();
                        
                        String temp=null;
                        while(true){
                            temp=Console.ReadLine();
                            //send message to pipeclient from console
                            sw.WriteLine(temp);
                            //it'll block util the other end of pipe has been read all send bytes
                            pipeServer.WaitForPipeDrain();
                            if(temp=="exit"){
                                break;
                            }
                        }
                    }
                }
                catch (IOException e) {
                    Console.WriteLine("[server] exception:{0}",e.Message);
                }
            }
            
            pipeClient1.WaitForExit();
            pipeClient1.Close();
        
        }
    }
}
PipeServer.cs

相关文章:

  • 2021-08-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
猜你喜欢
  • 2021-10-14
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案