`
jiq408694711
  • 浏览: 33100 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

C#使用匿名管道在本地进程之间进行通信

 
阅读更多

from CSDN

匿名管道提供的功能比命名管道少,但它需要的系统开销也少。您可以使用匿名管道更加轻松地在本地计算机上进行进程间通信。不能使用匿名管道通过网络进行通信。


下面的示例演示使用匿名管道将字符串从父进程发送到子进程的方式。此示例使用OutPipeDirection值在父进程中创建一个AnonymousPipeServerStream对象。然后,父进程通过使用客户端句柄创建一个AnonymousPipeClientStream对象来创建一个子进程。该子进程的In值为PipeDirection

然后,父进程将用户提供的字符串发送给子进程。该字符串将显示在子进程中的控制台上。

下面的示例演示服务器进程。

using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeServer
{
    static void Main()
    {
        Process pipeClient = new Process();

        pipeClient.StartInfo.FileName = "pipeClient.exe";

        using (AnonymousPipeServerStream pipeServer =
            new AnonymousPipeServerStream(PipeDirection.Out,
            HandleInheritability.Inheritable))
        {
            // Show that anonymous pipes do not support Message mode.
            try
            {
                Console.WriteLine("[SERVER] Setting ReadMode to \"Message\".");
                pipeServer.ReadMode = PipeTransmissionMode.Message;
            }
            catch (NotSupportedException e)
            {
                Console.WriteLine("[SERVER] Exception:\n    {0}", e.Message);
            }

            Console.WriteLine("[SERVER] Current TransmissionMode: {0}.",
                pipeServer.TransmissionMode);

            // Pass the client process a handle to the server.
            pipeClient.StartInfo.Arguments =
                pipeServer.GetClientHandleAsString();
            pipeClient.StartInfo.UseShellExecute = false;
            pipeClient.Start();

            pipeServer.DisposeLocalCopyOfClientHandle();

            try
            {
                // Read user input and send that to the client process.
                using (StreamWriter sw = new StreamWriter(pipeServer))
                {
                    sw.AutoFlush = true;
                    // Send a 'sync message' and wait for client to receive it.
                    sw.WriteLine("SYNC");
                    pipeServer.WaitForPipeDrain();
                    // Send the console input to the client process.
                    Console.Write("[SERVER] Enter text: ");
                    sw.WriteLine(Console.ReadLine());
                }
            }
            // Catch the IOException that is raised if the pipe is broken
            // or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("[SERVER] Error: {0}", e.Message);
            }
        }

        pipeClient.WaitForExit();
        pipeClient.Close();
        Console.WriteLine("[SERVER] Client quit. Server terminating.");
    }
}

下面的示例演示客户端进程。服务器进程启动客户端进程,并为该进程提供一个客户端句柄。应该将从客户端代码得到的可执行文件命名为pipeClient.exe并在运行该服务器进程之前将其复制到服务器可执行文件所在的目录中。

using System;
using System.IO;
using System.IO.Pipes;

class PipeClient
{
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            using (PipeStream pipeClient =
                new AnonymousPipeClientStream(PipeDirection.In, args[0]))
            {
                // Show that anonymous Pipes do not support Message mode.
                try
                {
                    Console.WriteLine("[CLIENT] Setting ReadMode to \"Message\".");
                    pipeClient.ReadMode = PipeTransmissionMode.Message;
                }
                catch (NotSupportedException e)
                {
                    Console.WriteLine("[CLIENT] Execption:\n    {0}", e.Message);
                }

                Console.WriteLine("[CLIENT] Current TransmissionMode: {0}.",
                   pipeClient.TransmissionMode);

                using (StreamReader sr = new StreamReader(pipeClient))
                {
                    // Display the read text to the console
                    string temp;

                    // Wait for 'sync message' from the server.
                    do
                    {
                        Console.WriteLine("[CLIENT] Wait for sync...");
                        temp = sr.ReadLine();
                    }
                    while (!temp.StartsWith("SYNC"));

                    // Read the server data and echo to the console.
                    while ((temp = sr.ReadLine()) != null)
                    {
                        Console.WriteLine("[CLIENT] Echo: " + temp);
                    }
                }
            }
        }
        Console.Write("[CLIENT] Press Enter to continue...");
        Console.ReadLine();
    }
}



分享到:
评论

相关推荐

    C# 使用管道Pipe在进程间通信

    进程间通信的一种方式,Pipes:管道,分为无名管道:在父子进程间交换数据;有名管道:可在不同主机间交换数据,分为服务器方和客户方,在Win9X下只支持有名管道客户。 1、进程间通信 2、管道Pipe通信

    C#与C++进程间通信

    通过命名管道实现了C#及C++进程的通信,并支持复制类型数据结构的传输.

    C#进程间通信

    操作系统实验之进程间通信(管道方式)。 内涵实验源码及报告文档。 更详尽内容请参考小魏博客:http://blog.csdn.net/xiaowei_cqu/article/details/7041212

    C#使用SendMessage实现进程间通信的方法

    本文实例讲述了C#使用SendMessage实现进程间通信的方法。分享给大家供大家参考。具体分析如下: 为了深入理解消息机制,先来做一个测试项目 在新建项目的Form1的代码中,加入方法: protected override void ...

    C# 进程间通信 共享内存

    特别提醒:共享内存并未提供同步机制,也就是说,在第一个进程结束对共享内存的写操作之前,并无自动机制可以阻止第二个进程开始对它进行读取,所以我们通常需要用其他的机制来同步对共享内存的访问,例如信号量。...

    c#进程之间通信,c#exe 之间发消息,c#exe 相互通信

    c#进程之间通信,c#exe 之间发消息,c#exe 相互通信 vs2005 编写的 接收 发送都有

    C# 进程间通信:命名管道方式例子

    C# 进程间通信:命名管道方式例子 .net 2.0框架环境,进程间传递对象。

    C# 进程间通信 Windows消息通讯,SendMessage

    Windows进程之间是相互独立的,通过Windows消息机制,我们可以在进程之间进行通信,适合一台电脑的windows平台下进行消息交换,该例是两个winform之间通过windows消息发送和接收数据。 1、进程间通信 2、SendMessage...

    C#进程间通信-管道代码实例

    C#进程间通信-管道代码实例。用实现了2个进程,一个Client 一个Server他们之间使用管道方式进行通信。

    C#进程间通信之共享内存

    利用共享内存实现进程间的通信,可用于操作系统的教学。(原创)

    C# 命名管道通信 NamedPipe 进程间通信

    C# NamedPipe 通信,管道通信。 目前还有些BUG ,但是用作程序间的数据通信,问题应该不大,建议用于 Json 通信。 做这玩意出来,起初想法是用作 winService 和 winform 的通信,可以通过winfrom 上的操作,来控制...

    C#实现进程间通信(使用消息队列实现)

    C#实现进程间通信(使用消息队列实现) 做得比较简单,但是基本实现了功能

    C# IPC 之 Socket 进程间通信 源码

    C#进程间怎样通信呢?IPC可以利用Socket网络通信。附录为C# Client-Server端 源代码及其详细注释,不用调试,直接可以使用测试。

    C#进程间通信-消息队列代码实例

    C#进程间通信-消息队列代码实例。用实现了2个进程,他们之间使用消息队列方式进行通信。

    C#使用共享内存实现进程间的通信

    进程通信有多种方式,比如socket、管道、共享内存。c#直接提供了共享内存的相关库,但直接使用起来还是不太方便,需要使用Marshal处理内存对齐以及托管非托管转换的问题,本文提供一种,将上述操作包装,借助反射的...

    C#命名管道通信,与QT命名管道通信

    命名管道通信,与QT命名管道通信,C#与C#的通信

    QT之进程和进程间通信(IPC)

    进程是操作系统的基础之一。...在 Qt 中,我们使用QProcess来表示一个进程。这个类可以允许我们的应用程序开启一个新的外部程序,并且与这个程序进行通讯。下面我们用一个非常简单的例子开始我们本章有关进程的阐述。

    C#进程之间通信ProcessSendMsg

    C#进程之间通信ProcessSendMsg 京华志&精华志出品 希望大家互相学习,互相进步 支持CSDN 支持微软 主要包括C# ASP.NET SQLDBA 源码 毕业设计 开题报告 答辩PPT等

    QT进程多个管道通信,并与C#客户端同时多个通信

    QT多个命名管道通信,并与C#客户端同时多个通信, 同时已有C#的客户端与服务器的通信,QT与QT的通信,QT与C#的通信,只要把管道名改为一至即可。

Global site tag (gtag.js) - Google Analytics