博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# NamePipe使用小结
阅读量:6954 次
发布时间:2019-06-27

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

最近在一次项目中使用到了C#中命名管道,所以在此写下一篇小结备忘。

为什么要使用命名管道呢?为了实现两个程序之间的数据交换。假设下面一个场景。在同一台PC上,程序A与程序B需要进行数据通信,此时我们就可以使用命名管道技术来实现。命名管道的两个对象。和 对象。请求通信的一方为Client端,发送数据的一方为Server端。

使用NamedPipe来通信,如果Server端崩溃了,不会影响到客户端。

下面我们通过一个例子来说明:

Server端:

UI:

Code:

private NamedPipeServerStream _pipe;        private const string PipeName = "PipeSample"; private const int PipeInBufferSize = 4096; private const int PipeOutBufferSize = 65535; private Encoding encoding = Encoding.UTF8; public MainWindow() { InitializeComponent(); _pipe = new NamedPipeServerStream ( PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous | PipeOptions.WriteThrough, PipeInBufferSize, PipeOutBufferSize ); _pipe.BeginWaitForConnection(WaitForConnectionCallback, _pipe); } private void WaitForConnectionCallback(IAsyncResult ar) { var pipeServer = (NamedPipeServerStream)ar.AsyncState; pipeServer.EndWaitForConnection(ar); var data = new byte[PipeInBufferSize]; var count = pipeServer.Read(data, 0, PipeInBufferSize); if (count > 0) { // 通信双方可以约定好传输内容的形式,例子中我们传输简单文本信息。 string message = encoding.GetString(data, 0, count); Dispatcher.BeginInvoke(new Action(() => { tblRecMsg.Text = message; })); } } private void OnSend(object sender, RoutedEventArgs e) { if (_pipe.IsConnected) { try { string message = txtSendMsg.Text; byte[] data = encoding.GetBytes(message); _pipe.Write(data, 0, data.Length); _pipe.Flush(); _pipe.WaitForPipeDrain(); } catch { } } Close(); }

Client端:

UI:

Code:

private const string PipeServerName = "PipeServer.exe"; private const string PipeName = "PipeSample"; private Encoding encoding = Encoding.UTF8; private NamedPipeClientStream _pipe; private bool _starting = false; public MainWindow() { InitializeComponent(); } private void OnConnect(object sender, RoutedEventArgs e) { if (_starting) { return; } var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, PipeServerName); var startInfo = new ProcessStartInfo(path) { UseShellExecute = false, CreateNoWindow = true }; try { var process = Process.Start(startInfo); _pipe = new NamedPipeClientStream ( ".", PipeName, PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough ); _pipe.Connect(); _pipe.ReadMode = PipeTransmissionMode.Message; string message = "Connected!"; byte[] data = encoding.GetBytes(message); _pipe.BeginWrite(data, 0, data.Length, PipeWriteCallback, _pipe); _starting = true; } catch (Exception ex) { Debug.Write(ex.StackTrace); } } private void PipeWriteCallback(IAsyncResult ar) { var pipe = (NamedPipeClientStream)ar.AsyncState; pipe.EndWrite(ar); pipe.Flush(); pipe.WaitForPipeDrain(); var data = new byte[65535]; var count = pipe.Read(data, 0, data.Length); if (count > 0) { string message = encoding.GetString(data, 0, count); Dispatcher.BeginInvoke(new Action(() => { tblRecMsg.Text = message; })); } }

需要注意的地方:因为我们在同一台PC上面进行通信,我们只需要将 NamedPipeClientStream 构造参数中pipeServer设为"."即可。另外因为这只是一个示例,所以PipeServer中只传递简单String类型。当然也可以传递其他类型的内容。

运行效果:

感谢您的阅读!

转载于:https://www.cnblogs.com/yang-fei/p/4696689.html

你可能感兴趣的文章
C# 多线程详解 Part.02(UI 线程和子线程的互动、ProgressBar 的异步调用)
查看>>
基于shiro授权过程
查看>>
JQuery对象和DOM对象的区别与转换
查看>>
使用 Toad 实现 SQL 优化
查看>>
.NET开发技巧——从Winform穿越到WPF
查看>>
2135亿背后的双11项目协作怎么玩?
查看>>
DRDS SQL 审计与分析——全面洞察 SQL 之利器
查看>>
微信小程序:模板消息推送实现
查看>>
CodePush自定义更新弹框及下载进度条
查看>>
自己总结的php开发中用到的工具
查看>>
小程序视频或音频自定义可拖拽进度条
查看>>
PHP导出超大的CSV格式的Excel表方案
查看>>
Mac 环境下如何生成Git shh key
查看>>
jenkins 使用磁盘检查插件 disk check plugin
查看>>
使用 Ruby 拓展 Vim
查看>>
centos7下安装LNMP(nginx+PHP7.1.9+mysql5.7)
查看>>
NodeAPI学习之Buffer
查看>>
深入java单例模式
查看>>
create-react-app
查看>>
20170812-XSS跨站脚本攻击
查看>>