zoukankan      html  css  js  c++  java
  • 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(); 

    }

  • 相关阅读:
    记账本第二天
    记账本第一天
    HDU 1811
    Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
    KMP超强模板贴一份
    2014辽宁省赛总结
    Codeforces Round #244 (Div. 2)
    CodeForces 383D Antimatter
    NEU 1351 Goagain and xiaodao's romantic story I
    UVA 10692 Huge Mod
  • 原文地址:https://www.cnblogs.com/jasonjiang/p/1764491.html
Copyright © 2011-2022 走看看