.net 3.5 remoting编程入门三 -尊龙游戏旗舰厅官网
vs2008 .net 3.5 remoting编程入门三
信道
什么是信道?信道有哪些类型呢?
信道顾名思意就是通信的通道。就想那些宣传标语说的,“要想富,先修路!”。同理,要学习remoting,当然要学习信道了。
.net framework 远程处理基础结构提供下列信道实现:
ipcchannel
tcpchannel
httpchannel
ipcchannel
ipcchannel 类使用命名管道为同一台计算机上的多进程应用程序提供高速进程间通信。ipcchannel 执行下列功能:
使用命名管道在发送方和接收方之间通信。
支持以二进制格式和行业标准 soap 序列化格式编码负载。
生成并使用对象引用的 channeldatastore。
支持模拟和委托。
支持在命名管道上利用访问控制列表 (acl) 来提供高级访问控制。
当一个应用程序必须与同一台计算机上其他进程中运行的另一个应用程序通信时,就应当使用 ipcchannel。由于 ipcchannel 使用命名管道,因此应用程序通常能够获得最高的通信性能,并可以使用模拟和委托来控制对远程对象的访问。对于三层应用程序而言,其第二层和第三层之间的通信必须能够在负载下运行良好。此时,这一功能便尤其有用。
tcpchannel
tcpchannel 类使用二进制格式化程序将所有消息序列化为二进制流,并使用 tcp 协议将该流传输至目标统一资源标识符 (uri)。tcpchannel 执行下列功能。
使用 tcp 套接字在发送方和接收方之间通信。
支持以二进制格式和行业标准 soap 序列化格式编码负载。
生成并使用对象引用的 channeldatastore。
支持模拟和委托。
支持 sspi 加密。
tcpchannel 会根据该时刻向另一台服务器发送请求的线程数打开并缓存相应数目的连接。当套接字连接的非活动时间超过 15-20 秒后,客户端将关闭这些连接。
在生成大量使用 .net framework 远程处理的应用程序时,很容易错误地使用 httpchannel 连接至用 tcpchannel 进行侦听的服务器应用程序域。如果建立了这种连接,客户端将收到以下异常:“基础连接已经关闭: 接收时发生错误”。如果您的客户端收到此异常,则应当检查客户端和服务器,以确定是否存在信道不匹配的问题。
httpchannel
httpchannel 类使用 soap 协议在远程对象之间传输消息。所有消息都通过 soapformatter 传递,此格式化程序会将消息转换为 xml 并进行序列化,同时向数据流中添加所需的 soap 标头。如果还指定了二进制格式化程序,则会创建二进制数据流。随后,将使用 http 协议将数据流传输至目标 uri。httpchannel 符合 soap 1.1 标准,它执行下列功能:
通过将 http 协议用作传输在发送方和接收方之间通信。
支持以 soap(一种 xml 编码标准)和二进制格式编码负载。
将接收方设置为通过 asp.net 和 tcp 套接字接收 http 请求并发送 http 响应。
生成并使用对象引用的 channeldatastore。
支持模拟和委托。
支持 sspi 加密。
httpchannel 一次只向给定服务器打开指定数目的连接。默认值为 2,但您可以使用应用程序配置文件中的 clientconnectionlimit 属性更改该默认值。
在生成大量使用 .net framework 远程处理的应用程序时,很容易错误地使用 httpchannel 连接至用 tcpchannel 进行侦听的服务器应用程序域。如果建立了这种连接,客户端将收到以下异常:“基础连接已经关闭: 接收时发生错误”。如果您的客户端收到此异常,则应当检查客户端和服务器,以确定是否存在信道不匹配的问题。
以上摘自msdn,已经说的很明白了。
每种类型的channel都有对应的clientchannel和serverchannel两中形式。在一般情况下,我们直接使用一种类型的channel,负责接受和发送。
下面看看代码怎么写的。
远程对象类,remotingclass.cs和以前的一样,不要改变。
using system;
namespace remotingclass
{
public class message : marshalbyrefobject
{
public guid messageid { get; set; } //guid,用于保存message的id
public message()
{
this.messageid = guid.newguid();//当对象被实例同时产生一个id
}
public delegate void messagehandler(string msg);
public static event messagehandler onsendmessage;
public void sendmessage(string msg)
{
if (onsendmessage != null)
onsendmessage("message的id是:" this.messageid msg);
}
}
}
服务端代码:(注意看注释的地方)
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.ipc;//使用ipcchannel
//using system.runtime.remoting.channels.http;//使用httpchannel
//using system.runtime.remoting.channels.tcp;//使用tcpchannel
namespace server
{
class program
{
static void main(string[] args)
{
console.writeline("host started!");
//httpchannel channel = new httpchannel(20001);//创建httpchannel通道
//tcpchannel channel = new tcpchannel(20001);//创建tcpchannel通道
ipcchannel channel = new ipcchannel("localhost:20001");//创建ipcchannel通道,和http、tcp有点不一样,其他的地方基本一样。
channelservices.registerchannel(channel, false);
remotingconfiguration.registerwellknownservicetype(typeof(remotingclass.message), "message.rem", wellknownobjectmode.singlecall); //这里使用singlecall即可
remotingclass.message.onsendmessage = new remotingclass.message.messagehandler(message_onsendmessage);
console.read();
}
public static void message_onsendmessage(string msg)
{
console.writeline(msg);
}
}
}
客户端代码:(注意看注释的方法)
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.runtime.remoting;
using system.runtime.remoting.channels;
using system.runtime.remoting.channels.ipc;
//using system.runtime.remoting.channels.http;//使用httpchannel
//using system.runtime.remoting.channels.tcp;//使用tcpchannel
namespace client
{
class program
{
static void main(string[] args)
{
console.writeline("client started!");
// httpchannel channel = new httpchannel();//创建httpchannel通道
// tcpchannel channel = new tcptchannel();//创建tcpchannel通道
ipcchannel channel = new ipcchannel();//创建ipcchannel通道
channelservices.registerchannel(channel, false);
remotingconfiguration.registerwellknownclienttype(typeof(remotingclass.message), "ipc://localhost:20001/message.rem");
//注意此处的协议,不同的通道使用不同的协议。如果是httpchannel,则使用http,如果是tcpchannel,则请使用tcp
remotingclass.message msg = new remotingclass.message();
while (true)
{
msg.sendmessage(" 现在是:" system.datetime.now.tostring());
system.threading.thread.sleep(2000);
}
//console.readline();
}
}
}
总结
以上是尊龙游戏旗舰厅官网为你收集整理的.net 3.5 remoting编程入门三的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: