windows管道程序客户端和服务器端类
//一、工具类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Pipes;
namespace ws.NamePipe
{
public class Client
{
string str_ServerName = "";
string str_pipename = "";
/// <summary>
/// 构造函数
/// ws.NamePipe.Client clt = new ws.NamePipe.Client(".", "test");
/// clt.SendMessage(this.textBox1.Text);
/// </summary>
/// <param name="ls_ServerName">服务器名字或IP</param>
/// <param name="ls_PipeName">管道名字</param>
public Client(string ls_ServerName,string ls_PipeName)
{
str_ServerName = ls_ServerName;
str_pipename = ls_PipeName;
}
/// <summary>
/// 先管道发送消息
/// </summary>
/// <param name="msg"></param>
public void SendMessage(string msg)
{
using (System.IO.Pipes.NamedPipeClientStream stream = new System.IO.Pipes.NamedPipeClientStream(this.str_ServerName, this.str_pipename))
{
stream.Connect();
byte[] bytes = Encoding.UTF8.GetBytes(msg);
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
stream.Close();
}
}
}
public class NamePipeEventArgs : EventArgs
{
string str_msg;
public string Str_msg
{
get
{
return this.str_msg;
}
}
public NamePipeEventArgs(string ls_msg)
{
this.str_msg = ls_msg;
}
}
public delegate void NamePipedelegate(NamePipeEventArgs e);
public class Server
{
private Thread MainThread;
private bool b;
/// <summary>
/// 管道收到消息后,要异步执行的事件
/// </summary>
public event NamePipedelegate NamePipe;
string str_pipename = "";
/// <summary>
/// 构造函数
/// ws.NamePipe.Server sver = new ws.NamePipe.Server("test");
/// sver.NamePipe += new ws.NamePipe.NamePipedelegate(sver_NamePipe);
/// sver.Start();
/// </summary>
/// <param name="pipename">管道名字</param>
public Server(string pipename)
{
str_pipename = pipename;
}
/// <summary>
/// 开始侦听
/// </summary>
public void Start()
{
MainThread = new Thread(new ThreadStart(_Start));
MainThread.Start();
}
/// <summary>
/// 结束侦听
/// </summary>
public void Stop()
{
b = false;
MainThread.Abort();
}
void _Start()
{
b = true;
while (b)
{
try
{
using (NamedPipeServerStream serverStream = new NamedPipeServerStream(this.str_pipename, PipeDirection.InOut, 10, PipeTransmissionMode.Message))
{
serverStream.WaitForConnection();//堵塞管道
Decoder _decoder = Encoding.UTF8.GetDecoder();
string msg = string.Empty;
while (serverStream.IsConnected)
{
int read;
byte[] bytes = new byte[16];
char[] chars = new char[16];
do
{
read = serverStream.Read(bytes, 0, bytes.Length);
if (read > 0)
{
int getCharCount = _decoder.GetChars(bytes, 0, read, chars, 0);
msg += new string(chars, 0, getCharCount);
}
}
while (!serverStream.IsMessageComplete);
Thread.Sleep(100);
}
//
//FileSystem.CreateOrAppend(str_logfile, msg);
//执行委托
if (this.NamePipe != null)
{
//异步
foreach (NamePipedelegate t in NamePipe.GetInvocationList())
{
NamePipeEventArgs a = new NamePipeEventArgs(msg);
t.BeginInvoke(a, null, null);
}
}
}
}
catch (Exception ex)
{
}
}
}
}
}
//在windows服务程序中的应用--
ws.NamePipe.Server sver;
protected override void OnStart(string[] args)
{
//thread_();
sver = new ws.NamePipe.Server("test");
sver.NamePipe += new ws.NamePipe.NamePipedelegate(sver_NamePipe);
sver.Start();
}
void sver_NamePipe(ws.NamePipe.NamePipeEventArgs e)
{
FileSystem.CreateOrAppend(str_logfile,e.Str_msg);
}
//客户端发送消息
private void button1_Click(object sender, EventArgs e)
{
ws.NamePipe.Client clt = new ws.NamePipe.Client(".", "test");
clt.SendMessage(this.textBox1.Text);
}