`
北极的。鱼
  • 浏览: 162128 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

【转】Remoting

    博客分类:
  • C#
 
阅读更多

转自: http://blog.csdn.net/changtianshuiyue/article/details/8871597

 

涉及到的类: 

客户端:

1.System.Runtime.Remoting.Channel.Tcp.TcpClientChannel类:为远程调用实现使用 TCP 协议传输消息的客户端信道。

信道跨越远程处理边界(例如,计算机或应用程序域)传输消息。TcpClientChannel 类使用 TCP 协议传输消息。

.NET Framework 远程处理基础结构使用信道传输远程调用。当客户端调用远程对象时,该调用即被序列化为一个消息,该消息通过客户端信道发送并通过服务器信道接收。然后将其反序列化并进行处理。所有返回值都通过服务器信道传输,并通过客户端信道接收。

2.System.Runtime.Remoting.Channel.ChannelService类:提供帮助进行远程处理信道注册、解析和 URL 发现的静态方法。无法继承此类。

3.System.Activator类:包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用。无法继承此类。

服务器端:

1.System.Runtime.Remoting.Channel.ChannelService类:提供帮助进行远程处理信道注册、解析和 URL 发现的静态方法。无法继承此类。

2.System.Runtime.Remoting.Channel.Tcp.TcpServerChannel类:为远程调用实现使用 TCP 协议传输消息的服务器信道。

3.System.Runtime.Remoting.RemotingConfiguration类:提供多种配置远程处理结构的静态方法。

4.System.Runtime.Remoting.WellKnownObjectMode枚举:定义如何激活已知对象。

 

  成员名称 说明
  SingleCall 每个传入的消息由新的对象实例提供服务。 
  Singleton 每个传入的消息由同一个对象实例提供服务。 

步骤:

 

1.设计自己的远程对象类:RemoteObject.

2.服务器端把远程对象用RemotingConfiguration.RegisterWellKnownServiceType方法注册为WellKnownServiceTypeEntry实例

3.服务器端注册TcpServerChannel到ChannelService中

4.客户端注册新的TcpClientChannel到ChannelService中。

5.客户端用Activator类从远程服务器的URL获取远程对象。

6.客户端使用远程对象。

远程对象代码:

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

namespace RemoteSampleLib
{
    public class RemoteObject : MarshalByRefObject
    {
        public RemoteObject()
        {
            Console.WriteLine("New RemoteObject Added!");
        }

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

 服务器端代码:

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

namespace RemoteServer
{
    class RemoteServer
    {
        static void Main(string[] args)
        {
            TcpServerChannel channel = new TcpServerChannel(6666);
            ChannelServices.RegisterChannel(channel);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteSampleLib.RemoteObject), "RemoteObject", WellKnownObjectMode.SingleCall);
            System.Console.WriteLine("Press Any Key");
            System.Console.ReadLine();
        }
    }
}

 客户端代码:

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

namespace RemoteClient
{
    class RemoteClient
    {
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new TcpClientChannel());
            RemoteObject remoteobj = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "tcp://localhost:6666/RemoteObject");
            Console.WriteLine("1+2=" + remoteobj.Add(1, 2).ToString());
            Console.ReadLine();
        }
    }
}

 注意远程对象要实现MarshalByRef类来允许远程对象被跨程序边界访问。

分享到:
评论
2 楼 北极的。鱼 2015-03-25  
转自:http://www.cnblogs.com/xia520pi/archive/2011/11/02/2233371.html
  什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式  。从微软的产品角度来看,可以说Remoting就是DCOM的一种升级,它改善了很多功能,并极好的融合到.Net平台下。Microsoft .NET Remoting 提供了一种允许对象通过应用程序域与另一对象进行交互的框架。这也正是我们使用Remoting的原因。为什么呢?在Windows操作系统中,是将应用程序分离为单独的进程。这个进程形成了应用程序代码和数据周围的一道边界。如果不采用进程间通信(RPC)机制,则在一个进程中执行的代码就不能访问另一进程。这是一种操作系统对应用程序的保护机制。然而在某些情况下,我们需要跨过应用程序域,与另外的应用程序域进行通信,即穿越边界。
  在Remoting中是通过通道(channel)来实现两个应用程序域之间对象的通信的。首先,客户端通过Remoting,访问通道以获得服务端对象,再通过代理解析为客户端对象。这就提供一种可能性,即以服务的方式来发布服务器对象。远程对象代码可以运行在服务器上(如服务器激活的对象和客户端激活的对象),然后客户端再通过Remoting连接服务器,获得该服务对象并通过序列化在客户端运行。
  在Remoting中,对于要传递的对象,设计者除了需要了解通道的类型和端口号之外,无需再了解数据包的格式。但必须注意的是,客户端在获取服务器端对象时,并不是获得实际的服务端对象,而是获得它的引用。这既保证了客户端和服务器端有关对象的松散耦合,同时也优化了通信的性能。
1 楼 北极的。鱼 2015-03-25  
要想在服务端和客户端传递数据,有2种方法:  
1. 传递的值可以支持序列化,
2. 继承自System.MarshalByRefObject  
假如一个数据被序列化传递到了客户端——那么,客户端接收到的,就是一个拷贝的副本。这时修改客户端的对象的值,服务端将不会被影响;(序列化的过程将对象转换了一种格式,便于传输,到了客户端之后,再将便于传输的格式还原,这时还原的对象和服务端的对象只是两个一摸一样的不同对象)。传输的是实际数据,所以要求传输对象[Serializable]).
假如选择后者,那么服务端的对象将这个对象的引用(指针)序列化传输到客户端,客户端反序列化得到了这个指针,这个指针指向的是服务端的对象;所以客户端修改这个对象将会修改到服务端。当然,Remoting的方式绝不是我说的“远程指针”这么简单,我的这种说法只是便于理解;(传输的是指针,所以和传输对象是不是[Serializable]没有关系)

相关推荐

Global site tag (gtag.js) - Google Analytics