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

 

 

 

 

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

 

客户端开始调用远程的类

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

 

 

 

总结:

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

Remoting 学习一调用远程的类就像调用本地的类一样的更多相关文章

  1. .Net Remoting 调用远程对象

    根据需求,我们的系统必须以C/S方式构建,而且是三层架构,这样一来,就出现了服务器端和客户端通信的问题. 为了解决双方的通信问题,还要考虑效率.性能等方面,经过分析.试验,我们根据效率.移植.开发难易 ...

  2. RabbitMQ入门学习系列(七) 远程调用RPC

    快速阅读 生产者和消费者启动以后,都有一个接收事件,消费者是接收事件是处理调用方法以后等待生产者的返回,生产者的接收事件是处理接收生产者发送的消息,进行处理.消费者发送的时候要在回调队列中加入一个标识 ...

  3. .NET Remoting学习笔记(二)激活方式

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科  ♂风车车.Net 激活方式概念 在 ...

  4. .NET Remoting学习笔记(一)概念

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 背景 自接触编程以来,一直听过这个名词Remotin ...

  5. 【转载】.NET Remoting学习笔记(二)激活方式

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在访 ...

  6. 【转载】.NET Remoting学习笔记(一)概念

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 背景 自接触编程以来,一直听过这个名词Remotin ...

  7. .NET Remoting学习笔记(三)信道

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:♂风车车.Net .NET Framework ...

  8. WebService学习整理(一)——客户端三种调用方式整理

    1 WebService基础 1.1 作用 1,       WebService是两个系统的远程调用,使两个系统进行数据交互,如应用: 天气预报服务.银行ATM取款.使用邮箱账号登录各网站等. 2, ...

  9. 【转载】.NET Remoting学习笔记(三)信道

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:♂风车车.Net .NET Framework ...

随机推荐

  1. [转载]$(document).ready(function(){});

    转载自:http://www.cnblogs.com/king-sheng/archive/2012/01/06/2313980.html $(document).ready(function() 页 ...

  2. 简述OSI七层协议模型、TCP/IP四层模型和五层协议之间的关系

    一.OSI七层模型 OSI七层协议模型主要是:应用层(Application).表示层(Presentation).会话层(Session).传输层(Transport).网络层(Network).数 ...

  3. 2、hive的基本操作

    1.创建数据库和表 1)创建数据库 hive> CREATE DATABASE IF NOT EXISTS userdb; OK Time taken: 0.252 seconds hive&g ...

  4. SSH终端显示中文乱码

    出现这种关系,首先想到是因为字符集不匹配导致的.打开SSH客户端,连接到linux虚拟机 在虚拟机中输入#cd /etc#cd sysconfig/ 找到i18ncat i18n 会显示当前的编码类型 ...

  5. Inception 2.0

    文章<Rethinking the Inception Architecture for Computer Vision> 介绍 VGG与GoogLeNet相比更朴素,但计算量大.Goog ...

  6. 2015.7.2 想做T再次失败

    2015.7.2教训:不要心存侥幸! 1.昨天收盘急跌,加上看到成交量在增加,负荷庄家行为第五条,一时脑热就去抄了.其实在震荡行情下,第二天肯定有时间点比头一天的收盘价低(Pic1) 2.T+0原则: ...

  7. Django 补充models操作,中间件, 缓存,信号,分页

    1.Model 一对多 补充 models如下: class UserType(models.Model): caption = models.CharField(max_length=16) cla ...

  8. TOGAF和BABOK

  9. Matlab命令合集 妈妈再也不用担心我不会用matlab了

    matlab命令 一.常用对象操作:除了一般windows窗口的常用功能键外.1.!dir 可以查看当前工作目录的文件. !dir& 可以在dos状态下查看.2.who 可以查看当前工作空间变 ...

  10. The Collections Module内建collections集合模块

    https://www.bilibili.com/video/av17396749/?p=12 Python函数式编程中的迭代器,生成器详解 课程内容 1.iterators are objects ...