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. pc端监听屏幕实现导航固定定位

    要点:占位符 js,监听屏幕滚动事件,当滚动条距离浏览器顶部的距离 大于 要固定定位开始以下元素的距离,则给要固定元素添加fixed样式. 初始化方法时,要给占位符添加样式 function sort ...

  2. 斯坦福大学Andrew Ng - 机器学习笔记(2) -- 逻辑回归 & 正则化

    大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...

  3. 使用Kotlin开发Android应用(III):扩展函数和默认值

    通过前面两篇文章,我们学习了Kotlin的基本知识,并知道如何配置工程,本文将接着介绍Java没有的而Kotlin实现了的有趣的特性.记住当你对Kotlin语言有任何疑问时,请参考官方指南.该指南组织 ...

  4. Binary Search in Java

    关于折半查找中的几个注意点. Version 1: public static <T extends Comparable<? super T>> int binSearch( ...

  5. mysql分组查询

    有一张学生选课表 Table: Subject_Selection Subject Semester Attendee --------------------------------- ITB001 ...

  6. Loadrunder脚本篇——web_submit_data实现提交post请求

    概述 web_link()和web_url()函数都是页面访问型函数,实现HTTP请求中的GET方法,如果需要实现POST方法,可使用web_submit_form或web_submit_data() ...

  7. LeetCode: Keyboard Row

    代码长了些,但还是比较简单的 public class Solution { public String[] findWords(String[] words) { List<String> ...

  8. Ag-grid控件使用pine:left后,配合iview下拉框,会出现闪烁

    Ag-grid控件使用pinned:left后,配合iview下拉框,会出现闪烁 引起原因:下拉图标的反转动画 目前解决方案: 添加一个全局样式: 禁用动画,其他地方也是如此, 影响控件有:gz-tr ...

  9. Docker容器技术-基础与架构

    一.什么是容器 容器是对应用程序及其依赖关系的封装. 1.容器的优点 容器与主机的操作系统共享资源,提高了效率,性能损耗低 容器具有可移植性 容器是轻量的,可同时运行数十个容器,模拟分布式系统 不必花 ...

  10. nodejs文件追加内容

    const fs = require("fs"); // fs.appendFile 追加文件内容 // 1, 参数1:表示要向那个文件追加内容,只一个文件的路径 // 2, 参数 ...