利用IPC通道进行进程间通信(C#)
2013-11-26 10:52
483 查看
有一个解决方案,其中包括一个Windows服务和一个Windows应用程序,两者之间需要进行通信。查了下,可以使用多种方法,如Web service(适用于不同系统及跨平台情况)、.NET Remoting、消息队列、WCF(集成了前述方法的功能,但太新,不支持Windows2000及以前的系统),其中Remoting可以支持TCP、HTTP、IPC通道的通信,而IPC通道速度快,且仅能供处于同一个系统中的进程间进行通讯,而这正好符合本项目的要求,故决定采用.NET Remoting的IPC方法:
开发平台为Visual Studio 2005,.NET framework版本为2.0
1、 建立空白解决方案
2、 添加两个新工程项目Console Application:Server和Client
3、 添加一个Class Library:RemoteObject
4、 三个项目的源代码Server.cs、Client.cs、RemoteObject.cs分别附后
5、 在Server和Client项目的引用中添加两个程序集:
System.Runtime.Remoting程序集(.net组件)和RemoteObject程序集(工程组件)
6、 生成两个工程之后,双击打开Server程序,再打开Client,可以看到两者通信。
7、 程序逻辑很简单:
Client根据约定好的对象地址(URI)“ipc://ServerChannel/RemoteObject”去服务器上访问RemoteObject对象。Server在收到访问对象的消息之后,实例化一个RemoteObject对象。当Client得到生成的RemoteObject对象句柄后,调用其Greeting方法,这个调用被传递到Server端执行(因为RemoteObject对象实际上是在Server上),然后返回Greeting方法的结果给Client。
8、 简言之,即完成了Client进程访问Server进程的一个对象,并调用此对象的方法的任务。
9、 源代码:
RemoteObject.cs
Server.cs
Client.cs
Server输出:
press return to exit
Constructor called
Greeting called
Constructor called
Greeting called
Constructor called
Greeting called
Constructor called
Greeting called
Client输出:
Hello,mmpire
Hello,mmpire
Hello,mmpire
Hello,mmpire
参考:<C#高级编程>
转自:http://blog.csdn.net/mmpire/article/details/2408578
开发平台为Visual Studio 2005,.NET framework版本为2.0
1、 建立空白解决方案
2、 添加两个新工程项目Console Application:Server和Client
3、 添加一个Class Library:RemoteObject
4、 三个项目的源代码Server.cs、Client.cs、RemoteObject.cs分别附后
5、 在Server和Client项目的引用中添加两个程序集:
System.Runtime.Remoting程序集(.net组件)和RemoteObject程序集(工程组件)
6、 生成两个工程之后,双击打开Server程序,再打开Client,可以看到两者通信。
7、 程序逻辑很简单:
Client根据约定好的对象地址(URI)“ipc://ServerChannel/RemoteObject”去服务器上访问RemoteObject对象。Server在收到访问对象的消息之后,实例化一个RemoteObject对象。当Client得到生成的RemoteObject对象句柄后,调用其Greeting方法,这个调用被传递到Server端执行(因为RemoteObject对象实际上是在Server上),然后返回Greeting方法的结果给Client。
8、 简言之,即完成了Client进程访问Server进程的一个对象,并调用此对象的方法的任务。
9、 源代码:
RemoteObject.cs
using System; public class RemoteObject : MarshalByRefObject { public RemoteObject() { Console.WriteLine("Constructor called"); } public string Greeting(string name) { Console.WriteLine("Greeting called"); return "Hello," + name; } } |
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Ipc; public class Server { public static void Main(string[] args) { //Instantiate our server channel. IpcServerChannel channel = new IpcServerChannel("ServerChannel"); //Register the server channel. ChannelServices.RegisterChannel(channel, false); //Register this service type. RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject", WellKnownObjectMode.SingleCall); Console.WriteLine("press return to exit"); Console.ReadLine(); } } |
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Ipc; public class Client { public static void Main(string[] args) { //Create an IPC client channel. IpcClientChannel channel = new IpcClientChannel(); //Register the channel with ChannelServices. ChannelServices.RegisterChannel(channel, false); RemoteObject obj = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "ipc://ServerChannel/RemoteObject"); if (obj == null) { Console.WriteLine("could not locate server"); return; } for (int i = 1; i < 5; i++) { Console.WriteLine(obj.Greeting("mmpire")); } } } |
press return to exit
Constructor called
Greeting called
Constructor called
Greeting called
Constructor called
Greeting called
Constructor called
Greeting called
Client输出:
Hello,mmpire
Hello,mmpire
Hello,mmpire
Hello,mmpire
参考:<C#高级编程>
转自:http://blog.csdn.net/mmpire/article/details/2408578
相关文章推荐
- 利用IPC通道进行进程间通信(C#)
- 利用IPC通道进行进程间通信(C#)
- 利用IPC通道进行进程间通信(C#)
- 利用IPC通道进行进程间通信(C#)
- 利用AIDL来实现由Binder进行进程间通信(IPC)
- 利用AIDL来实现由Binder进行进程间通信(IPC)
- IPC之 - C#用 IPC通道 进程间通信
- 利用AIDL来实现由Binder进行进程间通信(IPC)
- 利用C#进行AutoCAD的二次开发(三)(转自明经通道)
- 利用AIDL来实现由Binder进行进程间通信(IPC)
- C#中利用剪贴板进行进程间通信
- 利用C#进行AutoCAD的二次开发(二)
- IPC之 - C#中用Socket 实现进程间通信
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- 如何利用C#代码来进行操作AD
- android与PC,C#与Java 利用protobuf 进行无障碍通讯【Socket】
- Android IPC机制(四)用ContentProvider进行进程间通信
- 在C#中利用SharpZipLib进行文件的压缩和解压缩
- C#下利用高精度计时器进行计时操作【转】
- 在C#中利用SharpZipLib进行文件的压缩和解压缩