IPC之 - C#中用NamedPipe进程间通信
来源:互联网 发布:外贸常用聊天软件 编辑:程序博客网 时间:2024/06/15 05:10
C#中用NamedPipe进程间通信的测试例子,
核心代码是kernel32.dll中的一组windows api函数,这里不深入研究,代码都在codeproject上。
Inter-Process Communication in .NET Using Named Pipes, Part 1
测试效果如下,可以做到aspx和给console app发送消息后得到反馈:
console app为服务器端代码如下
usingSystem;
usingAppModule.InterProcessComm;
usingAppModule.NamedPipes;
usingSystem.Threading;
namespaceServer
{
classProgram
{
//**c#中用namedpipe进程间通信
//**组件代码来自codeproject
//**http://www.codeproject.com/KB/threads/dotnetnamedpipespart1.aspx
//**下载上面链接中的代码,编译AppModule.InterProcessComm和AppModule.NamedPipes两个dll
//**引用这两个dll到本例中,运行如下代码作为服务器端测试
//**测试代码byjinjazz(因为原作者的两个测试程序比较复杂,这里简化后供大家参考)
staticvoidMain(string[]args)
{
ServerPipeConnectionPipeConnection=newServerPipeConnection("PChostName
",512,512,5000,false);
Console.WriteLine("listening..");
while(true)
{
try
{
PipeConnection.Disconnect();
PipeConnection.Connect();
stringrequest=PipeConnection.Read();
if(!string.IsNullOrEmpty(request))
{
Console.WriteLine("get:"+request);
PipeConnection.Write("get:"+request);
if(request.ToLower()=="break")break;
}
}
catch(Exceptionex)
{
Console.WriteLine(ex.Message);
break;
}
}
PipeConnection.Dispose();
Console.Write("pressanykeytoexit..");
Console.Read();
}
}
}
客户端的aspx代码如下
usingSystem;
usingSystem.Web;
usingAppModule.InterProcessComm;
usingAppModule.NamedPipes;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
Response.Write(SendRequest("测试asdf"));
}
///<summary>
///测试namepiped客户端
///</summary>
///<paramname="request">发送命令</param>
///<returns>返回数据</returns>
stringSendRequest(stringrequest)
{
stringresponse="";
IInterProcessConnectionclientConnection=null;
try
{
clientConnection=newClientPipeConnection("PChostName",".");
clientConnection.Connect();
clientConnection.Write(request);
response=clientConnection.Read();
clientConnection.Close();
}
catch(Exceptionex)
{
clientConnection.Dispose();
response=ex.Message;
}
returnresponse;
}
}
测试环境为windows XP、windows vista和windows2003
- IPC之 - C#中用NamedPipe进程间通信
- C#中用NamedPipe进程间通信
- IPC之 - C#中用WM_COPYDATA SendMessage 进程间通信
- IPC之 - C#中用Socket 实现进程间通信
- IPC之 - C#用 IPC通道 进程间通信
- 进程间通信之:IPC
- C# NamedPipe 命名管道通信
- NamedPipe,利用命名管道实现进程间通信
- 进程间通信(IPC)之管道
- android进程间通信(IPC)之AIDL
- 进程间通信IPC之--共享内存
- 进程间通信IPC之--共享内存
- 进程间通信IPC之--共享内存
- 进程间通信IPC之--共享内存
- Qt之进程间通信(IPC)
- Android进程间通信(IPC)之Messenger
- Android进程间通信(IPC)之AIDL
- 进程间通信之XSI IPC
- Java IO 操作与字节流
- lightbox 模板的简单修改
- spring 元单测试
- struts2 所需jar包详解
- 第一天上来,留个印子
- IPC之 - C#中用NamedPipe进程间通信
- Spr咖啡的创业之道
- struts2 + spring + hibernate 所需的jar包
- Wireshark插件开发小结
- gzip 命令例子
- 优秀网站源码
- linux的chmod与chown命令详解
- startUML导入Ogre类库成功
- Linux的mount命令简介