Remoting在Internet环境下的测试

本文探讨了在Internet环境下Remoting技术的测试方法,重点介绍了单向与双向通道的配置方式,特别是对于位于不同网络环境的服务端如何通过路由器端口映射实现远程访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Remoting在Internet环境下的测试

                          朱二(2008.10,转载请保留作者)                          

 

分两种情况:单向通道和双向通道

在单向通道(Client->Server)情况下,分两种情况:
A、Server在内网,则需要在路由器上建立端口映射,同时在Server建立通道的时候,指定路由器IP地址
B、Server在公网,不需要任何设置

在双向通道(Client<->Server)情况下
Server和Client都具有双重角色,都可作为客户端和服务端,因此可等同对待。
无论哪一方在内网,都需要建立端口映射,并且在建立通道的时候,指定路由器IP地址。


相关代码:

跨路由访问内网远程服务端的问题
比如远程服务端的内网地址为192.168.0.8,端口为8888,在路由器地址为11.11.11.11,在路由器上添加端口映射,将路由器端口8888映射至192.168.0.8的端口8888
远程类型为:

  1. Public Class CRemotingLib
  2.     Inherits MarshalByRefObject
  3.     Public Function Test() As String
  4.         Return Now.ToString()
  5.     End Function
  6.     Public T As New CTest
  7. End Class
  8. Public Class CTest
  9.     Inherits MarshalByRefObject
  10.     Public Function Test() As String
  11.         Return Now.ToString()
  12.     End Function
  13. End Class

客户端代码为

  1.  Dim objectUrl As String = String.Format("tcp://{0}:{1}/CRemotingLib.rem""11.11.11.11",8888)
  2.  Dim root As RemotingHttpTest.CRemotingLib = Activator.GetObject(GetType(RemotingHttpTest.CRemotingLib), objectUrl)
  3.  Console.WriteLine(root.Test())'正常
  4.  Console.WriteLine(root.T.Test())'异常,不能正常执行,调试发现,客户端执行次句时会连接192.168.0.8:8888

解决办法:

服务端设置信道属性时使用路由器的IP地址

  1. Dim cbin As BinaryClientFormatterSinkProvider = New BinaryClientFormatterSinkProvider()
  2. Dim sbin As BinaryServerFormatterSinkProvider = New BinaryServerFormatterSinkProvider()
  3. sbin.TypeFilterLevel = Runtime.Serialization.Formatters.TypeFilterLevel.Full
  4. Dim properties As IDictionary = New Hashtable()
  5. properties("port") = 9111
  6. properties("machineName") = "11.11.11.11" '注意这一句,Server将IP地址指定为路由器地址,这样客户端会在后继调用时使用此地址连接Server 
  7. Dim channel As TcpChannel = New TcpChannel(properties, cbin, sbin)
  8. ChannelServices.RegisterChannel(channel, False)
  9. RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingHttpTest.CRemotingLib), "CRemotingLib.rem", WellKnownObjectMode.Singleton)

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值