博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows 匿名管道
阅读量:6038 次
发布时间:2019-06-20

本文共 3128 字,大约阅读时间需要 10 分钟。

 

匿名管道

The function creates an anonymous pipe and returns two handles: a read handle to the pipe and a write handle to the pipe. The read handle has read-only access to the pipe, and the write handle has write-only access to the pipe. To communicate using the pipe, the pipe server must pass a pipe handle to another process. Usually, this is done through inheritance; that is, the process allows the handle to be inherited by a child process. The process can also duplicate a pipe handle using the function and send it to an unrelated process using some form of interprocess communication, such as DDE or shared memory.

A pipe server can send either the read handle or the write handle to the pipe client, depending on whether the client should use the anonymous pipe to send information or receive information. To read from the pipe, use the pipe's read handle in a call to the function. The ReadFile call returns when another process has written to the pipe. The ReadFile call can also return if all write handles to the pipe have been closed or if an error occurs before the read operation has been completed.

To write to the pipe, use the pipe's write handle in a call to the function. The WriteFile call does not return until it has written the specified number of bytes to the pipe or an error occurs. If the pipe buffer is full and there are more bytes to be written, WriteFile does not return until another process reads from the pipe, making more buffer space available. The pipe server specifies the buffer size for the pipe when it calls .

Server Write

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))        {            pipeClient.StartInfo.Arguments =                pipeServer.GetClientHandleAsString();            pipeClient.StartInfo.UseShellExecute = false;            pipeClient.Start();            pipeServer.DisposeLocalCopyOfClientHandle();            using (StreamWriter sw = new StreamWriter(pipeServer))            {                sw.AutoFlush = true;                Console.Write("Enter text: ");                sw.WriteLine(Console.ReadLine());            }        }        pipeClient.WaitForExit();        pipeClient.Close();    }}

Client Read

class PipeClient{    static void Main(string[] args)    {        if (args.Length > 0)        {            using (PipeStream pipeClient =                new AnonymousPipeClientStream(PipeDirection.In, args[0]))            {                using (StreamReader sr = new StreamReader(pipeClient))                {                    string temp;                    while ((temp = sr.ReadLine()) != null)                    {                        Console.WriteLine(temp);                    }                }            }        }        Console.Write("Press Enter to continue...");        Console.ReadLine();    }}

参考:

都是简化的操作

转载地址:http://mkrhx.baihongyu.com/

你可能感兴趣的文章
第一阶段冲刺报告(一)
查看>>
使用crontab调度任务
查看>>
【转载】SQL经验小记
查看>>
zookeeper集群搭建 docker+zk集群搭建
查看>>
Vue2.5笔记:Vue的实例与生命周期
查看>>
论JVM爆炸的几种姿势及自救方法
查看>>
联合体、结构体简析
查看>>
使用throw让服务器端与客户端进行数据交互[Java]
查看>>
java反射与代理
查看>>
深度分析Java的ClassLoader机制(源码级别)
查看>>
微服务架构选Java还是选Go - 多用户负载测试
查看>>
我的友情链接
查看>>
Javascript中的异步如何实现回调
查看>>
halcon算子介绍
查看>>
挖掘你不知道的windowsxp中的带宽潜能
查看>>
Software Engineering 招聘要求
查看>>
【转载】InstallAnyWhere自动化制作安装包的知识
查看>>
69、iSCSI共享存储配置实战
查看>>
文本编程
查看>>
乔布斯走了。你还期待苹果吗?
查看>>