| 网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 |
![]() |
| Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道 |
IPCChannel是。NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。在同一计算机上的应用程序域之间进行通信时,IPC 信道比 TCP 或 HTTP 信道要快得多。但是IPC只在本机应用之间通信。所以,在客户端和服务端在同一台机器时,我们可以通过注册IPCChannel来提高Remoting的性能。但如果客户端和服务端不在同一台机器时,我们不能注册IPCChannel. 网管u家u.bitsCN.com
下面让我们来看看如何使用IPCChannel:
首先我们定义一个RemotingObject类:
中国网管论坛bbs.bitsCN.com
using System;
// 远程对象
public class RemoteObject : MarshalByRefObject
...{
private int callCount = 0;
public int GetCount()
...{
Console.WriteLine("GetCount has been called.");
callCount++;
return(callCount);
}
}
中国网管联盟bitsCN.com |
接下来我们编写服务端代码: 网管联盟bitsCN@com
using System;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
public class Server
...{
[SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] args)
...{
// 创建一个IPC信道
IpcChannel serverChannel = new IpcChannel("TestChannel");
// 注册这个IPC信道.
System.Runtime.Remoting.Channels.ChannelServices. |
客户端代码:
using System;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
public class Client
...{
[SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] args)
...{
// 创建一个IPC信道。
IpcChannel channel = new IpcChannel();
// 注册这个信道。
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel |
主要代码就算完成了。但,还有一个问题,那就是如果服务端和客户端在不同的Windows帐户运行的时候,会有验证权限的问题。对于这个问题,我们只要把服务端的信道注册代码改一下就好了: 网管bitscn_com
Hashtable ht = new Hashtable();
ht["portName"] = "TestChannel";
ht["name"] = "ipc";
ht["authorizedGroup"] = "Everyone";
serverChannel= new IpcChannel(ht, null, provider); |
|
0
|
评论加载中…