Remoting多个信道(Chennel)的注册问题(转)

一般情况下我们用Remoting一个信道应该就够用了,因为程序要么是客户端,要么是服务器端。但是有时候也会出现一个客户端需要连接多个服务器端的情况,或者一个程序既作为服务器端(针对内网),又作为客户端(针对外网)。这个时候就需要注册多个信道了。
  根据一般的经验,客户端信道和服务器端信道应该是不冲突的。但实际的情况呢?看一下以下的代码:
None.gif IChannel serverChannel  =   new  TcpServerChannel(  5000  ); 
None.gifChannelServices.RegisterChannel( serverChannel, 
true  ); 
None.gif
None.gifIChannel clientChannel 
=   new  TcpClientChannel(); 
None.gifChannelServices.RegisterChannel( clientChannel );
运行后会出现异常“信道 'tcp' 已注册。”(RemotingException)
注册两个客户端信道也一样会出现这个错误:

None.gif IChannel channel1  =   new  TcpClientChannel(); 
None.gifChannelServices.RegisterChannel( channel1, 
true  ); 
None.gifIChannel channel2 
=   new  TcpClientChannel(); 
None.gifChannelServices.RegisterChannel( channel2, 
true  ); 
开始我怀疑是端口冲突,给每个信道分别设置不同的端口:
None.gif Hashtable props1  =   new  Hashtable(); 
None.gifprops1[
" port " =   5001
None.gifIChannel channel1 
=   new  TcpClientChannel( props1,  new  BinaryClientFormatterSinkProvider() ); 
None.gifChannelServices.RegisterChannel( channel1, 
true  ); 
None.gifHashtable props2 
=   new  Hashtable(); 
None.gifprops2[
" port " =   5002
None.gifIChannel channel2 
=   new  TcpClientChannel( props2,  new  BinaryClientFormatterSinkProvider() ); 
None.gifChannelServices.RegisterChannel( channel2, 
true  ); 
错误依旧。想想也是,如果端口冲突,应该是这种错误:“通常每个套接字地址(协议/网络地址/端口)只允许使用一次。”(SocketException)
  再分析一下原来的错误:“信道 'tcp' 已注册。”。难道是信道的名字冲突?
  赶紧把channel的ChannelName打印出来看一下:
  Console.WriteLine( "The Default Channel Name is " + (new TcpClientChannel()).ChannelName );
  "The Default Channel Name is tcp"...

  问题找到。接下来要做的就是在注册不同信道的时候,显式指定其信道名称。ServerChannel和ClientChannel各有不同的方法,以下示例其一:
None.gif IChannel channel1  =   new  TcpClientChannel(  " Channel1 " new  BinaryClientFormatterSinkProvider() ); 
None.gifChannelServices.RegisterChannel( channel1, 
true  ); 
None.gifIChannel channel2 
=   new  TcpClientChannel(  " Channel2 " new  BinaryClientFormatterSinkProvider() ); 
None.gifChannelServices.RegisterChannel( channel2, 
true  ); 
BTW:因为很少看到网上Remoting的文章提到多信道的注册,所以把这个贴出来。也许大家注册信道的时候就指定了名字,这样就不会有这个问题。emteeth.gif 另外,以上均是在.NET 2.0平台上。

  MSDN上的相关说明:
  ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpguide/html/cpconchannels.htm
  "信道名称在应用程序域中必须是唯一的。例如,由于默认信道具有名称,因此,若要在一个应用程序域中注册两个 HttpChannel 对象,就必须在注册它们之前更改信道的名称。"

转载于:https://www.cnblogs.com/HurYun/archive/2006/10/19/533656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值