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

}

 

转载于:https://www.cnblogs.com/jasonjiang/archive/2010/06/24/1764491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值