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 ...
随机推荐
- 基础篇-java开发
开局必知 1.变量 在java中,以{}为作用域,所以就存在成员变量和局部变量之说 由于java是强类型语言,所以在申明变量的时候,必须指定类型 java里,一个变量有声明过程和初始化过程(也就是赋值 ...
- python 元类metaclass
文章转自:http://www.cnblogs.com/linhaifeng/articles/8029564.html 一 知识储备 exec:三个参数 参数一:字符串形式的命令 参数二:全局作用域 ...
- ThreadPoolExecutor 线程池任务队列分析 与 利特尔法则(Little's law)
一. 演示 public class ThreadPoolTest { static class MyThread implements Runnable { private String name; ...
- python requests 使用
快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...
- php异常处理类
<?php header('content-type:text/html;charset=UTF-8'); // 创建email异常处理类 class emailException extend ...
- 转:USB枚举
- json学习笔记--在JavaScript中的使用
1.字符串转换为JavaScript对象 var jsonStr = '[' + '{"name":"陶国荣","sex":"男& ...
- Vuex的入门教程
前言 在 Vue.js 的项目中,如果项目结构简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式,详细点击这篇文章查看. 但是如果是大型项目,很多时候都需要在子组件之间传递 ...
- [转]AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
AOP那些学术概念—通知.增强处理连接点(JoinPoint)切面(Aspect) 1.我所知道的AOP 初看起来,上来就是一大堆的术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充 ...
- debian内核代码执行流程(二)
继续上一篇文章<debian内核代码执行流程(一)>未完成部分. acpi_bus_init调用acpi_initialize_objects,经过一系列复杂调用后输出下面信息: [ IN ...