Remoting

 

Microsoft .Net Remoting系列专题之一:.Net Remoting基础篇

Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命周期以及跟踪服务

Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触

系列文章二代码:

RemoteObject类:

using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting.Lifetime; using System.Runtime.Remoting.Services; using System.Runtime.Remoting; namespace RemoteObject { public class RemoteObject1 : MarshalByRefObject { public void PrintString(string contents) { Console.WriteLine(contents); } public override object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.RenewOnCallTime = TimeSpan.FromSeconds(20); } return lease; } } public class RemoteObject2 : MarshalByRefObject { public void PrintString(string contents) { Console.WriteLine(contents); } public override object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(3); lease.RenewOnCallTime = TimeSpan.FromSeconds(40); } return lease; } } public class MyTracking:ITrackingHandler { public MyTracking() { } public void MarshaledObject(object obj,ObjRef or) { Console.WriteLine(); Console.WriteLine("对象" + obj.ToString() + " is marshaled at " + DateTime.Now.ToShortTimeString()); } public void UnmarshaledObject(object obj,ObjRef or) { Console.WriteLine(); Console.WriteLine("对象" + obj.ToString() + " is unmarshaled at " + DateTime.Now.ToShortTimeString()); } public void DisconnectedObject(object obj) { Console.WriteLine(obj.ToString() + " is disconnected at " + DateTime.Now.ToShortTimeString()); } } }  

Server端:

using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Lifetime; using System.Runtime.Remoting.Services; using RemoteObject; namespace RemoteServer { class RemoteServer { static void Main(string[] args) { MyTracking tracking = new MyTracking(); TcpServerChannel channel = new TcpServerChannel(8888); ChannelServices.RegisterChannel(channel); LifetimeServices.LeaseTime = TimeSpan.Zero; TrackingServices.RegisterTrackingHandler(tracking); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject1), "RemoteObject1", WellKnownObjectMode.Singleton); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject2), "RemoteObject2", WellKnownObjectMode.Singleton); Console.WriteLine("Remoting服务启动,按退出..."); Console.ReadLine(); } } }  

Client端:

using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemoteObject; namespace RemoteClient { class RemoteClient { static void Main(string[] args) { TcpClientChannel channel = new TcpClientChannel(); ChannelServices.RegisterChannel(channel); RemoteObject1 obj1 = (RemoteObject1)Activator.GetObject(typeof(RemoteObject1), "TCP://localhost:8888/RemoteObject1"); obj1.PrintString("远程对象1"); RemoteObject2 obj2 = (RemoteObject2)Activator.GetObject(typeof(RemoteObject2), "TCP://localhost:8888/RemoteObject2"); obj2.PrintString("远程对象1"); Console.ReadLine(); } } }  

 

你可能感兴趣的:(.net,String,object,tcp,Microsoft,Class)