您的位置:首页 > 编程语言 > C#

C# .NET Remoting 简单应用示例

2008-03-24 20:17 981 查看
这个是Remoting中的HelloWorld


using System;




namespace zhl




...{


public class Person:MarshalByRefObject




...{


private string m_name;




public string Name




...{


get




...{


return this.m_name;


}


set




...{


this.m_name = value;


}


}


public string 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




...{


static void 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




...{


static void 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();


}


}


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: