有一个解决方案,其中包括一个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. Pig store用法举例

    store:将数据存储到HDFS等文件系统里   将数据保存到/data目录 store data into '/data'; 以逗号为分隔符 store data into '/data' usin ...

  2. Ubuntu16安装GPU版本TensorFlow(个人笔记本电脑)

    想着开始学习tf了怎么能不用GPU,网上查了一下发现GeForce GTX确实支持GPU运算,所以就尝试部署了一下,在这里记录一下,避免大家少走弯路. 使用个人笔记本电脑thinkpadE570,内存 ...

  3. 软工读书笔记 week 7 ——《构建之法》

    总时长近两周的结对项目终于算是结束了,马上要重新开启团队项目.于是这几天决定对<构建之法>一书中与团队项目及需求分析有关的章节进行重点阅读,希望能够从中得到启发,并运用到接下来的团队项目中 ...

  4. leveldb源码分析--Key结构

    [注]本文参考了sparkliang的专栏的Leveldb源码分析--3并进行了一定的重组和排版 经过上一篇文章的分析我们队leveldb的插入流程有了一定的认识,而该文设计最多的又是Batch的概念 ...

  5. MySQL Group Replication配置

    MySQL Group Replication简述 MySQL 组复制实现了基于复制协议的多主更新(单主模式). 复制组由多个 server成员构成,并且组中的每个 server 成员可以独立地执行事 ...

  6. ORACLE DBA应该掌握的9个免费工具

      TOP1 : 录像机OS Watcher 如果说,作为一个Oracle维护工程师,你至少应该装一个工具在你维护的系统里,那么我首推这个.它就像银行自助取款机顶上的摄像头,默默的记录下你操作系统中的 ...

  7. asp.net MVC4 框架揭秘 读书笔记系列1

    1.1 传统MVC 名词解释 Autonomous View. AV. 自制视图 GUI图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式 ...

  8. [翻译] UIGlossyButton

    UIGlossyButton https://github.com/waterlou/UIGlossyButton Feature create standard iPhone buttons wit ...

  9. win7 64 &vs2010 与 opengl配置

    http://blog.csdn.net/lixam/article/details/7618015 http://blog.sina.com.cn/s/blog_7745fc8601017m36.h ...

  10. 【2017-01-08】QTimer与QThread的调度时间精度

    在最近的项目开发中,我发现有的人喜欢用QThread来实现需要循环执行的工作流,而有的人又喜欢用QTimer来实现. 在表面上,两种实现方式似乎都可以,但我觉得QTimer的精度可能会有问题,首先看一 ...