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();
}
}
}
开始调用,首先启动服务端程序实现注册端口和服务

客户端开始调用远程的类

查看本地端口,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 学习一调用远程的类就像调用本地的类一样的更多相关文章
- .Net Remoting 调用远程对象
根据需求,我们的系统必须以C/S方式构建,而且是三层架构,这样一来,就出现了服务器端和客户端通信的问题. 为了解决双方的通信问题,还要考虑效率.性能等方面,经过分析.试验,我们根据效率.移植.开发难易 ...
- RabbitMQ入门学习系列(七) 远程调用RPC
快速阅读 生产者和消费者启动以后,都有一个接收事件,消费者是接收事件是处理调用方法以后等待生产者的返回,生产者的接收事件是处理接收生产者发送的消息,进行处理.消费者发送的时候要在回调队列中加入一个标识 ...
- .NET Remoting学习笔记(二)激活方式
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在 ...
- .NET Remoting学习笔记(一)概念
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 背景 自接触编程以来,一直听过这个名词Remotin ...
- 【转载】.NET Remoting学习笔记(二)激活方式
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在访 ...
- 【转载】.NET Remoting学习笔记(一)概念
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 背景 自接触编程以来,一直听过这个名词Remotin ...
- .NET Remoting学习笔记(三)信道
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:♂风车车.Net .NET Framework ...
- WebService学习整理(一)——客户端三种调用方式整理
1 WebService基础 1.1 作用 1, WebService是两个系统的远程调用,使两个系统进行数据交互,如应用: 天气预报服务.银行ATM取款.使用邮箱账号登录各网站等. 2, ...
- 【转载】.NET Remoting学习笔记(三)信道
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:♂风车车.Net .NET Framework ...
随机推荐
- MySQL版本与工具
MySQL各个版本区别 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Serve ...
- es6数组的一些函数方法使用
数组函数forEach().map().filter().find().every().some().reduce()等 数组函数(这里的回调函数中的index和arr都可以省略,回调函数后有参数是设 ...
- 数据库之MySQL(三)
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 临时表搜索 SELECT *FRO ...
- 前端基础-html(2)
一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong>.<em>.<sup>.<sub&g ...
- 斯坦福大学Andrew Ng - 机器学习笔记(4) -- 机器学习算法的选择与评估
大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...
- StringBuffer、StringBuilder
相信大家都知道StringBuffer.StringBuilder,但是这两个的用法都差不多,到底有什么区别呢,这也是面试的时候问的比较多的一道题,这里我就来说说,这两个的区别结合String来说~ ...
- SE80 开发对象
偶然发现:开发对象可以自动识别对象的类别.
- 剑指offer 面试51题
面试51题: 题目:数组中的逆序对 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007 ...
- 剑指offer 面试61题
面试61题: 题目:LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到 ...
- iOS学习之库
一.什么是库 库是程序代码的集合,是共享程序代码的一种方式. 二.库的分类 根据源代码的公开情况,库可以分为2种类型. 1.开源库 公开源代码,能看到具体实现. 比如,SDWebImage.AFNet ...