• Remoting 学习一调用远程的类就像调用本地的类一样


    Remoting

    使用TCP/IP 协议,服务端可以是服务,web服务器,类。

    例子1.  远程调用服务端的类,就像调用客户端机器上的类一样。

    服务端代码 (先定义被客户端调用的类,然后注册到某个端口中去,客户端访问刚才注册地址  ip:端口号/类名)

    1)类

    类实现加法运算

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
        public class RemotingSample:MarshalByRefObject
        {
            public RemotingSample()
            {
                Console.WriteLine("New References Added");
            }

            public int sum(int a, int b)
            {
                return a + b;
            }

        }
    }

    注:MarshalByRefObject   允许在支持远程处理的应用程序中跨应用程序域边界访问对象

    2)服务端控制端应用程序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime;

    using System.Runtime.Remoting.Channels.Tcp;           //引用中必须加入 System.Runtime.Remoting 才能使用
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                TcpServerChannel chanel = new TcpServerChannel(6666);
                ChannelServices.RegisterChannel(chanel);

                RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingSample), "RemotingSample", WellKnownObjectMode.SingleCall);
                Console.WriteLine("请按下任意键");

                Console.ReadKey();
            }
        }
    }

    3)客户端。控制端应用程序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.Runtime;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels.Tcp;
    using ConsoleApplication1;
    namespace Rclient
    {
        class Program
        {
            static void Main(string[] args)
            {
                ChannelServices.RegisterChannel(new TcpClientChannel(),true);   //本机调用true,false都可以

                RemotingSample remoteObj = (RemotingSample)(Activator.GetObject(typeof(RemotingSample), "tcp://localhost:6666/RemotingSample"));

                Console.WriteLine("1+2="+remoteObj.sum(1,2).ToString());
                Console.ReadKey();
            }
        }
    }

    开始调用,首先启动服务端程序实现注册端口和服务

    image

    客户端开始调用远程的类

    image

    查看本地端口,6668服务端口,22034是客户端主机随便分配的端口号。

    image

    总结:

    1.使用下面的命名空间

    using System.Runtime;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels.Tcp;

    2.

    注册TCP端口供客户端调用,且一个协议只能注册一个

    TcpServerChannel chanel = new TcpServerChannel(6668);  ///服务端注册tcp端口
    ChannelServices.RegisterChannel(chanel);

    接着注册Remoting服务

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingSample), "RemotingSample", WellKnownObjectMode.SingleCall);

    3. 客户端使用

    调用TCP://ip:服务端注册的端口号/服务端注册的类

    Console.WriteLine("1+2="+remoteObj.sum(1,2).ToString());

  • 相关阅读:
    烂笔头@WP 的博文仅供自己学习的备忘录
    工具---搜索
    ubuntu---安装teamviewer和向日葵
    Ubuntu---标注工具 XXX-labelImg
    ubuntu---深度学习标注工具CasiaLabeler
    ubuntu---kazam安装
    ubuntu---notepadqq安装
    ubuntu---TeamViewer安装
    ubuntu---鼠标的乌龙事件
    ubuntu---终端下不显示 中文文字 的解决方法
  • 原文地址:https://www.cnblogs.com/StudyLife/p/3556731.html
Copyright © 2020-2023  润新知