using System; namespace zhl ...{ publicclass Person:MarshalByRefObject ...{ privatestring m_name; publicstring Name ...{ get ...{ returnthis.m_name; } set ...{ this.m_name = value; } } publicstring SayHello() ...{ Console.WriteLine("Hello,my name is {0}",m_name); return"Hi,"+ m_name; } } }
Server:
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; namespace zhl ...{ class Server ...{ staticvoid Main(string[] args) ...{ TcpChannel channel =new TcpChannel(8000); ChannelServices.RegisterChannel(channel,true); RemotingConfiguration.RegisterWellKnownServiceType(typeof(Person), "PersonUrl", WellKnownObjectMode.Singleton); Console.WriteLine("Please press enter to exit"); Console.ReadLine(); } } }
Client:
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; namespace zhl ...{ class Client ...{ staticvoid Main(string[] args) ...{ TcpChannel channel =new TcpChannel(); ChannelServices.RegisterChannel(channel,true); Person person = (Person)Activator.GetObject(typeof(Person), "tcp://localhost:8000/PersonUrl"); person.Name ="zhao hongliang"; string reply = person.SayHello(); Console.WriteLine(reply); Console.WriteLine("Press enter to exit."); Console.ReadLine(); } } }