Remoting 技术

Remoting 是基于TCP连接的,要实现她要先创建一个类库,然后将类库的dll文件引用到服务端和客户端

类库的主要代码:

创建一个类库叫Compute ,在类库中创建一个接口ICompute

ICompte的代码:

public interface ICompute

{

  int Mutil(int a, int b);

}

定义一个Mathes的类,必须继承一个支持远程访问的对象

public class Mathes : MarshalByRefObject , ICompute

{

  public Mathes()

  {

    Console.WriteLine("创建一个对象实例");

  }

  #region ICompute 成员

  public int Mutil(int a, int b)

  {

    return a * b;

  }

  #endregion

}

 

创建一个控制台程序

服务器的主要代码:

static void Main(string[] args)

{

  Console.WriteLine("服务器");

  //注册一个通道

  ChannelServices.RegisterChannel(new TCPServerChannel(50000));

  //注册一个类型

  RemotingConfiguration.RegisterWellKnownServiceType(typeof(Compute.Mathes),"abc",WellKnownObjectMode.Singleton);

  Console.Read();

}

 

客户端主要代码:

static void Main(string[] args)

{

  Console.WriteLine("客户端");

  //注册一个通道,不需要指定端口号

  ChannelServices.RegisterChannel(new TcpChannel());

  //获取类型的实例

  Compute.Mathes ma = Activator.GetObject(typeof(Compute.Mathes),"tcp://127.0.0.1:50000/abc") as Compute.Mathes;

  int result = ma.Mutil(2,3);

  Console.WriteLine(result);

 

  Console.Read(); 

}

 

posted @ 2010-06-24 15:56  JasonNET  阅读(293)  评论(0编辑  收藏  举报