网站首页 > 教程分享 正文
1 新建解决方案NamedPipeExample
在解决方案下面新建两个项目:Client和Server,两者的输出类型均为“Windows 应用程序”。整个程序的结构如下图所示。
2 实现项目Client
Client仅包含一个名为“客户端”的窗体,如下图所示。
编写窗体后端代码,如下所示。
using System;
using System.IO;
using System.IO.Pipes;
using System.Security.Principal;
using System.Windows.Forms;
namespace Client
{
public partial class frmClient : Form
{
NamedPipeClientStream pipeClient =
new NamedPipeClientStream("localhost", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.None);
StreamWriter sw = null;
public frmClient()
{
InitializeComponent();
}
private void frmClient_Load(object sender, EventArgs e)
{
try
{
pipeClient.Connect(5000);
sw = new StreamWriter(pipeClient);
sw.AutoFlush = true;
}
catch (Exception ex)
{
MessageBox.Show("连接建立失败,请确保服务端程序已经被打开。");
this.Close();
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (sw != null)
{
sw.WriteLine(this.txtMessage.Text);
}
else
{
MessageBox.Show("未建立连接,不能发送消息。");
}
}
}
}
3 实现项目Server
Server项目仅包含一个名为“服务端”的窗体,如下图所示。
编写窗体后端代码,如下所示。
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Windows.Forms;
namespace Server
{
public partial class frmServer : Form
{
NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.InOut,1,PipeTransmissionMode.Message,PipeOptions.Asynchronous);
public frmServer()
{
InitializeComponent();
}
private void frmServer_Load(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate
{
pipeServer.BeginWaitForConnection((o) =>
{
NamedPipeServerStream pServer = (NamedPipeServerStream)o.AsyncState;
pServer.EndWaitForConnection(o);
StreamReader sr = new StreamReader(pServer);
while (true)
{
this.Invoke((MethodInvoker)delegate { lsvMessage.Items.Add(sr.ReadLine()); });
}
}, pipeServer);
});
}
}
}
4 运行程序
运行Server.exe与Client.exe程序,效果如下图所示。
实例中共发送三次消息,分别传递数据1,2,3。
本例中演示的客户端和服务端程序均位于本地机器,使用命名管道可以与网络上的其他进程进行通信。
5、若要重复连接服务端,改造如下
public void ccc()
{
try {
pipeServer = new NamedPipeServerStream("testPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
ThreadPool.QueueUserWorkItem(delegate
{
pipeServer.BeginWaitForConnection((o) =>
{
NamedPipeServerStream pServer = (NamedPipeServerStream)o.AsyncState;
pServer.EndWaitForConnection(o);
StreamReader sr = new StreamReader(pServer);
string s = sr.ReadLine();
pServer.Dispose();
pipeServer.Dispose();
if (s != null)
{
this.Dispatcher.Invoke((System.Windows.Forms.MethodInvoker)delegate
{
if (this.Visibility != Visibility.Hidden) return;
this.Visibility = System.Windows.Visibility.Visible;
this.ShowInTaskbar = true;
this.Activate();
ccc();
});
}
}, pipeServer);
});
}
catch { }
}
- 上一篇: 使用C#编程判断某一年是否为闰年
- 下一篇: DotNet 入门:(一)环境安装
猜你喜欢
- 2025-05-14 C#开发串口通信实例及串口基础
- 2025-05-14 C#窗体多线程启动,暂停,继续,取消
- 2025-05-14 DotNet 入门:(一)环境安装
- 2025-05-14 使用C#编程判断某一年是否为闰年
- 2025-05-14 C#学习随笔—自定义控件(线,箭头等图形)
- 2025-05-14 UE4基础知识总结(三)
- 2025-05-14 C#使用 WinForms 实现打印基础操作
你 发表评论:
欢迎- 05-14C#开发串口通信实例及串口基础
- 05-14C#窗体多线程启动,暂停,继续,取消
- 05-14DotNet 入门:(一)环境安装
- 05-14C#中使用命名管道进行进程通信的实例
- 05-14使用C#编程判断某一年是否为闰年
- 05-14C#学习随笔—自定义控件(线,箭头等图形)
- 05-14UE4基础知识总结(三)
- 05-14C#使用 WinForms 实现打印基础操作
- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)