首页 技术 正文
技术 2022年11月21日
0 收藏 618 点赞 5,000 浏览 2418 个字

有一个解决方案,其中包括一个Windows服务和一个Windows应用程序,两者之间需要进行通信。查了下,可以使用多种方法,如Web service(适用于不同系统及跨平台情况)、.NET Remoting、消息队列、WCF(集成了前述方法的功能,但太新,不支持Windows2000及以前的系统),其中Remoting可以支持TCP、HTTP、IPC通道的通信,而IPC通道速度快,且仅能供处于同一个系统中的进程间进行通讯,而这正好符合本项目的要求,故决定采用.NET Remoting的IPC方法:

 开发平台为Visual Studio 2005,.NET framework版本为2.01、 建立空白解决方案2、 添加两个新工程项目Console Application:Server和Client3、 添加一个Class Library:RemoteObject4、 三个项目的源代码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;
}
}

Server.cs

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();
}
}

Client.cs

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"));
}
}
}

Server输出:press return to exitConstructor calledGreeting calledConstructor calledGreeting calledConstructor calledGreeting calledConstructor calledGreeting calledClient输出:Hello,mmpireHello,mmpireHello,mmpireHello,mmpire

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848