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

    }

  • 相关阅读:
    通过日期获得星期方法
    去掉标签方法
    获取当前时间方法
    根据身份证计算年龄方法
    查询日志
    Vagrant打造Laravel开发环境(LNMP)
    mysql创建索引以及进程过程中出现的问题
    array_to_sql
    Linux创建快捷方式(链接)命令ln
    Mac搭建PHP Phalcon框架
  • 原文地址:https://www.cnblogs.com/jasonjiang/p/1764491.html
Copyright © 2020-2023  润新知