有一个解决方案,其中包括一个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
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 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

利用IPC通道进行进程间通信(C#)的更多相关文章

  1. c#进程间通讯方案之IPC通道

    转载:http://www.cnphp.info/csharp-ipc-channel-remoting.html 最近一直纠结与使用多进程还是多线程来构建程序.多线程的方法似乎不错,但是一个进程可承 ...

  2. 消息队列,IPC机制(进程间通信),生产者消费者模型,线程及相关

    消息队列 创建 ''' Queue是模块multiprocessing中的一个类我们也可以这样导入from multiprocessing import Queue,创 建时queue = Queue ...

  3. 【IPC第二个进程间通信】管道Pipe

    IPC进程间通信+管道Pipe                IPC(Inter-Process Communication,进程间通信).         管道用于进程间共享数据,事实上质是共享内存 ...

  4. 一次穿墙渗透测试,利用IPC跨域

    Shell是怎么拿下的我们就不纠结了. 我们来上传菜刀一句话,来仔细分析分析. 先来看看内网环境把. 很高兴的是现在管理员在线.可以抓去文明密码. 但是很悲催的又是.服务器不支持走TCP协议.HTTP ...

  5. 在对方电脑建立IPC连接, 利用IPC$入侵 运行木马

    第一大步:  IPC漏洞的建立 1)在目标主机上设置组策略:開始->执行-〉gpedit.msc 2)计算机配置->windows配置-〉本地策略-〉安全选项 3)在安全选项中, 将网络訪 ...

  6. ipc - System V 进程间通信机制

    SYNOPSIS 总览 # include <sys/types.h> # include <sys/ipc.h> # include <sys/msg.h> # ...

  7. [转]Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile

    http://blog.csdn.net/stpeace/article/details/39534361 进程间的通信方式有很多种, 上次我们说了最傻瓜的“共享外存/文件”的方法. 那么, 在本文中 ...

  8. Linux IPC(Inter-Process Communication,进程间通信)之管道学习

    1.标准流管道 管道操作支持文件流模式,用来创建链接还有一个进程的管道,通过函数popen和pclose popen的详细介绍在本blog:Linux 多进程学习中有具体介绍 2.无名管道(PIPE) ...

  9. UNIX环境高级编程——创建与打开IPC通道

    创建或打开一个IPC对象的三个getXXX函数的第一个参数key是类型为key_t的IPC键,返回值identifier是一个整数标识符.该标识符不同于ftok函数的id参数.对于key值,应用程序有 ...

随机推荐

  1. python汉字转拼音

    上代码: #!/usr/bin/env python # -*- coding:utf-8 -*- """ Author:cleverdeng E-mail:clverd ...

  2. eclipse打成可运行jar包,清空运行路径选项

    到eclipse的工作空间找到/.metadata/.plugins/org.eclipse.debug.core/.launches文件夹,清空:然后重启eclipse即可:

  3. 回归JavaScript基础(八)

    主题:引用类型包装类.单体内置对象的介绍. 对于我们开发人员来说,JavaScript有种引用类型一定很陌生!那就是基本包装类型:Boolean.Number和String.这也不是我们的错,主要这些 ...

  4. leetCode Detect Capital

    1.问题描述 Given a word, you need to judge whether the usage of capitals in it is right or not. We defin ...

  5. 如何在首次启动 Linux 虚拟机时对其进行自定义

    在前面的教程中,你已学习如何通过 SSH 连接到虚拟机 (VM) 并手动安装 NGINX. 若要以快速一致的方式创建 VM,通常需要某种形式的自动化. 在首次启动 VM 时实现自定义的常见方法是使用  ...

  6. MVC 实现自定义404错误页

    直接进入正题. 在HomeController中有一个NotFound的Action方法. public ActionResult NotFound() { return View(); } 对应的视 ...

  7. ELT探索之旅2 kettle配置

    java环境变量配置: path增加    ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 双击spoon.bat即可

  8. Provisional headers are shown

    问题: Chrome请求出现"Provisional headers are shown": 原因: 这种一般是由于浏览器端的插件或客户端的软件对请求进行了拦截:我们出现的情况,是 ...

  9. GIT非常见命令使用笔记

    1:修改已经提交N次代码的user.name和user.email 解决我在多电脑间,使用不同账户,git config 的global,system,local配置忽略改动,而添加了多台电脑ssh ...

  10. CSS盒子模型之CSS3可伸缩框属性(Flexible Box)

    CSS盒子模型(下) 一.CSS3可伸缩框(Flexible Box) 可伸缩框属性(Flexible Box)是css3新添加的盒子模型属性,有人称之为弹性盒模型,它的出现打破了我们经常使用的浮动布 ...