分布式数据库系统---商旅预定系统的实现(3)
2013-06-27 23:00
148 查看
通信信道的建立过程
1.注册通道
Remoting技术的通信建立分为两种:服务器端激活和客户端激活。本次设计采用的是服务器端激活模式。在信道的建立过程中,需要在服务器端和客户端分别进行处理。
服务器端处理如下:
要跨越应用程序域进行通信,必须实现通道。如前所述,Remoting提供了IChannel接口,分别包含TcpChannel和HttpChannel两种类型的通道。这两种类型除了性能和序列化数据的格式不同外,实现的方式完全一致,因此下面我们就以TcpChannel为例。
注册TcpChannel,首先要在项目中添加引用“System.Runtime.Remoting”,然后using名字空间:System.Runtime.Remoting.Channel.Tcp。
代码如下:
static void Main(string[] args) { #region 数据表一(Car)的服务器启动 TcpServerChannel chanCar = new TcpServerChannel(9999); ChannelServices.RegisterChannel(chanCar, false); Console.WriteLine(chanCar.ChannelName); RemotingConfiguration.RegisterWellKnownServiceType (typeof(CarRemoteServiceImpl), "Service", WellKnownObjectMode.Singleton); Console.WriteLine("数据表一(Car)的服务器启动..."); #endregion Console.WriteLine("Press <Enter> to Exit..."); Console.ReadLine(); }
在实例化通道对象时,将端口号作为参数传递。然后再调用静态方法RegisterChannel()来注册该通道对象即可。
2.注册远程对象
注册了通道后,要能激活远程对象,必须在通道中注册该对象。根据激活模式的不同,注册对象的方法也不同。(1)SingleTon模式
对于WellKnown对象,可以通过静态方法RemotingConfiguration.RegisterWellKnownServiceType()来实现:
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(ServerRemoteObject.ServerObject),"ServiceMessage",WellKnownObjectMode.SingleTon);
(2)SingleCall模式
注册对象的方法基本上和SingleTon模式相同,只需要将枚举参数WellKnownObjectMode改为SingleCall就可以了。
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(ServerRemoteObject.ServerObject),"ServiceMessage",WellKnownObjectMode.SingleCall);
3.客户端处理
客户端主要做两件事,一是注册通道。Remoting中服务器端和客户端都必须通过通道来传递消息,以获得远程对象。第二步则是获得该远程对象。注册通道:
//初始化全局变量 TcpClientChannel chan; //分服务器声明 IHotelRemoteService hotelRemoteService; ICarRemoteService carRemoteService; IFlightRemoteService flightRemoteService; ICustomerRemoteService customerRemoteService; IReservationRemoteService reservationRemoteService;
chan = new TcpClientChannel(); ChannelServices.RegisterChannel(chan, false); #region 实例化远程对象 hotelRemoteService = (IHotelRemoteService)Activator.GetObject (typeof(IHotelRemoteService), "tcp://localhost:9998/Service", null); carRemoteService = (ICarRemoteService)Activator.GetObject (typeof(ICarRemoteService), "tcp://localhost:9999/Service", null); customerRemoteService = (ICustomerRemoteService)Activator.GetObject (typeof(ICustomerRemoteService), "tcp://localhost:9997/Service", null); flightRemoteService = (IFlightRemoteService)Activator.GetObject (typeof(IFlightRemoteService), "tcp://localhost:9996/Service", null); reservationRemoteService = (IReservationRemoteService)Activator.GetObject (typeof(IReservationRemoteService), "tcp://localhost:9995/Service", null); #endregion
注意在客户端实例化通道时,是调用的默认构造函数,即没有传递端口号。事实上,这个端口号是缺一不可的,只不过它的指定被放在后面作为了Uri的一部分。
获得远程对象。
与服务器端相同,不同的激活模式决定了客户端的实现方式也将不同。不过这个区别仅仅是WellKnown激活模式和客户端激活模式之间的区别,而对于SingleTon和SingleCall模式,客户端的实现完全相同。
WellKnown激活模式
要获得服务器端的知名远程对象,可通过Activator进程的GetObject()方法来获得:
ServerRemoteObject.ServerObject serverObj = (ServerRemoteObject.ServerObject)
Activator.GetObject( typeof
(ServerRemoteObject.ServerObject), "tcp://localhost:8080/ServiceMessage");
首先以WellKnown模式激活,客户端获得对象的方法是使用GetObject()。其中参数第一个是远程对象的类型。第二个参数就是服务器端的uri。如果是http通道,自然是用了。因为我是用本地机,所以这里是localhost,你可以用具体的服务器IP地址来代替它。端口必须和服务器端的端口一致。后面则是服务器定义的远程对象服务名,即ApplicationName属性的内容。
相关文章推荐
- 分布式数据库系统---商旅预定系统的实现(6)
- 分布式数据库系统---商旅预定系统的实现(10)
- 分布式数据库系统---商旅预定系统的实现(11)
- 分布式数据库系统---商旅预定系统的实现(2)
- 分布式数据库系统---商旅预定系统的实现(12)
- 分布式数据库系统---商旅预定系统的实现(1)
- 分布式数据库系统---商旅预定系统的实现(完结)
- 分布式数据库系统---商旅预定系统的实现(4)
- 分布式数据库系统---商旅预定系统的实现(5)
- 分布式数据库系统---商旅预定系统的实现(7)
- 分布式数据库系统---商旅预定系统的实现(8)
- 分布式数据库系统---商旅预定系统的实现(9)
- 系统下实现电脑服务器自动重启
- 模拟UNIX文件系统的设计及实现
- 如何实现一个文件系统(三)
- Android 系统中同时删除多条SMS的具体实现
- 第三篇:一个Spark推荐系统引擎的实现
- java Swing mysql实现的人事管理系统项目源码
- aidl实现Android系统语言切换
- 顺序表实现学生管理系统