zoukankan      html  css  js  c++  java
  • NET Remoting 示例

        .NET Remoting是.NET平台上允许存在于不同应用程序域中的对象相互知晓对方并进行通讯的基础设施。调用对象被称为客户端,而被调用对象则被称为服务器或者服务器对象。简而言之,它就是.NET平台上实现分布式对象系统的框架。只要是跨越AppDomain的访问,都属于Remoting。Remoting编程基本原理:当客户端创建远程RemotableClass的一个实例,.NET框架在客户端应用程序域中产生一个代理。该代理看起来就像实际对象。代理收到调用后,通过通道连接到远程的对象。

    Remoting通过通道(channel)来传输消息。Remoting的通道主要有两种:Tcp和Http。在.Net中,System.Runtime.Remoting.Channel中定义了IChannel接口。IChannel接口包括了TcpChannel通道类型和Http通道类型。它们分别对应Remoting通道的这两种类型。

    TcpChannel类型放在名字空间System.Runtime.Remoting.Channel.Tcp中。Tcp通道提供了基于Socket的传输工具,使用Tcp协议来跨越Remoting边界传输序列化的消息流。TcpChannel类型默认使用二进制格式序列化消息对象,因此它具有更高的传输性能。HttpChannel类型放在名字空间System.Runtime.Remoting.Channel.Http中。它提供了一种使用Http协议,使其能在Internet上穿越防火墙传输序列化消息流。默认情况下,HttpChannel类型使用Soap格式序列化消息对象,因此它具有更好的互操作性。通常在局域网内,我们更多地使用TcpChannel;如果要穿越防火墙,则使用HttpChannel。

     

     

     

    我自己写了一个示例,工程结构如下:

    1,创建一个类库,用来创建我们需要的接口

    namespace RemotingInterface
    {
        public interface MyInterface 
        {
            string Greeting(string sName);
        }
    }

    2,创建接口的实现,需要添加上面工程的引用

    namespace RemotingImp
    {
        public class MyClass : MarshalByRefObject, MyInterface
        {
    
            #region MyInterface 成员
    
            public string Greeting(string sName)
            {
                return "Hello!" + sName;
            }
    
            #endregion
        }
    }

    3, 服务器端实现,需要添加上面两个的引用

    namespace RemotingSever
    {
        class Program
        {
            static void Main(string[] args)
            {
                TcpChannel channel = new TcpChannel(8080);
                ChannelServices.RegisterChannel(channel, false);
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingImp.MyClass), "RemotingPersonService", WellKnownObjectMode.SingleCall);
    
                System.Console.WriteLine("Server:Press Enter key to exit");
                System.Console.ReadLine();
        
    
    
            }
        }
    }

    4,客户端,需要添加接口所在的类库

    namespace RemotingClient
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                TcpChannel channel = new TcpChannel();
                ChannelServices.RegisterChannel(channel, false);
                MyInterface obj = (MyInterface)Activator.GetObject(typeof(RemotingInterface.MyInterface), "tcp://localhost:8080/RemotingPersonService");
                if (obj == null)
                {
                    Console.WriteLine("Couldn't crate Remoting Object .");
                }
    
                Console.WriteLine("Please enter your name:");
                String name = Console.ReadLine();
                try
                {
                    Console.WriteLine(obj.Greeting(name));
                }
                catch (System.Net.Sockets.SocketException e)
                {
                    Console.WriteLine(e.ToString());
                }
                Console.ReadLine();
            }
    
        }
    }
  • 相关阅读:
    网上图书商城项目学习笔记-001工具类测试
    SpringMVC,MyBatis商品的增删改查
    3.Spring-用反射模拟IoC
    2.Sprng-IoC-Java反射例子
    1.Spring IoC简单例子
    HTML5 文件API(二)
    HTML5 文件API(一)
    HTML5 编辑 API 之 Range 对象(二)
    hdu 5384 Danganronpa
    hdu 3065 病毒侵袭持续中
  • 原文地址:https://www.cnblogs.com/zuiyirenjian/p/4172214.html
Copyright © 2011-2022 走看看