remoting与socket、web service的比较及实例

 

 http://blog.csdn.net/bigpudding24/article/details/50369904

remoting与socket、web service的比较及实例

标签: Remoting
 分类:
 

目录(?)[+]

 

 

 

remoting基础

 

       一种分布式处理方式,可以说是DCOM的一种升级

       跨过应用程序域,与另外的应用程序域进行通信,即穿越边界

       在remoting中是通过通道(channel)来实现两个应用程序域之间对象的通信的

      优点: 
      1、能让我们进行分布式开发 
      2、Tcp通道的Remoting速度非常快 
      3、虽然是远程的,但是非常接近于本地调用对象 
      4、可以做到保持对象的状态 
      5、没有应用程序限制,可以是控制台,winform,iis,windows服务承载远程对象 
     缺点: 
      1、非标准的应用,因此有平台限制 
      2、脱离iis的话需要有自己的安全机制

socket VS remoting

网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。

使用socket无疑是效率最高的。但是,在复杂的接口环境下,socket的开发效率也是最低的。故在兼顾开发效率的情况下,可以使用remoting来代替socket开发。并且:
    1、Tcp通道的Remoting速度非常快。
    你可以通过端口查看工具,发现remoting比直接socket传输的内容,应该是属于同一个数量级的。
    2、虽然是远程的,但是非常接近于本地调用对象。
    也就是完全符合面向对象思想。
    3、可以做到保持对象的状态
    直接使用socket传输机制,我们必须花大量的精力来处理异常、断网、死机等现象,使用remoting,这些工作会大大简化。Socket是用于通讯、传输数据的,Remoting主要是远程调用对象的。 .NET Remoting不过是建立在Socket上的高级应用   。。如果你是主要进行数据库操作,remoting适合你,而且控制起来比socket容易

 remoting vs webservice 

   1、remoting可寄宿在你自己的代码中,也可寄宿在windows服务及IIS中。最大程度的提供了开发和部署的灵活性。
   2、remoting在使用http通道的时候,也如web service一样支持穿透路由。但WebService是跨平台的,Java和.Net可以互相提供和引用对方的Web Service,.net remoting就限制于.net平台使用。
   3、remoting与web sercie相比,提供双向通信。哪怕是将remoting寄宿在IIS中,也支持。
   4、当然,web service最主要优势是,它是一个行业标准,而remoting只是微软自己内部的标准,如果你的应用要脱离微软的平台,就只能使用web service了。

  5、.net remoting是有状态的,是紧密耦合;web service是无状态的(因为http是无状态的),是松散耦合;总的来说remoting适合局域网内,对性能和响应效率要求较高的场合;而web service适合跨网络,跨系统,对移植性和通用性要求较高的场合。.net remoting在局域网上的表现绝对是大大强于web services,使用tcp管道不失真的传输数据,从而减轻了序列化和反序列化的工作,当然使用WEB服务的时候,一台计算机存储32位整数的方式与另一台计算机的存储方式是不同的,因此需要像XML这样易于理解的格式。web services 是IIS执行的,而.NET Remoting的扩展性强,使用HTTP信道和XML可以达到web services的技术的一部分,个人觉得可以把web service看成是.NET Remoting的一个特例。

总结

从性能上讲:Socket>Remoting(TCP通道)>WebService。
如果你是Windows进程间通讯的话,WebService可以不用考虑。
如果两个进程 在不同机器的话,可以看情况使用Socket或Remoting
如果是本机进程间通讯的话,那就用Remoting(IPC通道)
不过Remoting也要序列化和反序列化,所以在数据生成方面Remoting并没有优势。

使用socket效率比较高,但是最大的问题是需要硬编码,传过去的数据包需要按位解析。
使用Remoting比较好,传递的是类对象,不需要双方的协议了,而且基于TCP的Remoting效率也是很高的,.Net Framework帮你完成了序列化和反序列化。从程序可读和可维护性说,还是Remoting吧,但是个人感觉Remoting的事件不是很好用。

实例

参考《C#高级编程 第7版 第54章》

上图显示了客户端和服务器应用程序中的主要.NET Remoting 类。实现的远程对象是 Hello。HelloServer是服务器上应用程序的主类, HelloClient 是客户端上应用程序的主类。

 

远程对象

分布式计算需要远程对象。从不同系统中远程调用的对象必须派生自 System.MarshalByRefObject对象。为了查看起作用的.NET Remoting,下面给远程对象创建一个简单的类库(不能独立运行,因此无main()函数)。 Hello 类派生自System.MarshalByRefObject。在构造函数中,把消息写入控制台中,提供对象的生命周期信息。此外,添加一个从客户端调用的 Greeting()方法。

 

 

程序集的名称是 RemoteHello,类的名称是 Hello。
[csharp] view plain copy
 
  1. using System.Linq;  
  2. using System.Text;  
  3. using System.Threading.Tasks;  
  4.   
  5. namespace RemoteHello  
  6. {  
  7.     public class Hello:System.MarshalByRefObject   
  8.     {  
  9.         public Hello ()  
  10.         {  
  11.             Console.WriteLine("Constructor called");  
  12.         }  
  13.         public string Greeting(string name)  
  14.         {  
  15.             Console.WriteLine("Greeting called");  
  16.             return "Hello," + name;  
  17.         }  
  18.     }  
  19. }  

简单的服务器应用程序

对于服务器,创建一个新的 C#控制台应用程序 HelloServer。为了使用 TcpServerChannel 类,必须引用 System.Runtime.Remoting 程序集。此外,还需要引用上一节创建的 RemoteHello 程序集。
在 Main()方法中,用端口号 8086 创建一个 System.Runtime.Remoting.Channels.Tcp.TcpServerChannel类型的对象。该信道使用 System.Runtime.Remoting.Channels.ChannelServices 类注册,使之可用于远程对象。远程对象类型通过调用 RemotingConfiguration. RegisterWellKnownServiceType()方法注册。
[csharp] view plain copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Runtime.Remoting;  
  7. using System.Runtime.Remoting.Channels;  
  8. using System.Runtime.Remoting.Channels.Tcp;  
  9. using RemoteHello;  
  10.   
  11. namespace HelloServer  
  12. {  
  13.     class Program  
  14.     {  
  15.         static void Main(string[] args)  
  16.         {  
  17.             //在服务器端创建TcpServerChannel信道  
  18.             var channel = new TcpServerChannel(8086);  
  19.              
  20.             //注册该信道,使之可用于远程对象。  
  21.             ChannelServices.RegisterChannel(channel, true);  
  22.   
  23.             //用于为服务器激活的对象注册远程对象类型,(把知名的远程对象类型注册为 RemotingServices)  
  24.             //第一个参数是 typeof(Hello),它指定远程对象的类型。第二个参数 Hi 是远程对象的 URI, 客户端访问远程对象时要使用这个 URI。 最后一个参数是远程对象的模式。             
  25.             RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);  
  26.   
  27.             Console.WriteLine("Press return to exit");  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }  

简单的客户端应用程序

这个客户端应用程序也是一个 C#控制台应用程序 HelloClient。在该项目中,也引用System.Runtime. Remoting 程序集,以便可以使用 TcpClientChannel 类。此外,也必须引用 RemoteHello程序集。尽管将在远程服务器上创建对象,但是为了代理能在运行期间读取类型信息,还需要在客户端上引用程序集。
在客户端程序中,要创建一个 TcpClientChannel 对象,这个对象在 ChannelServices 中注册。对于TcpChannel,可以使用默认的构造函数,因此可以选择任意一个端口。接下来,使用 Activator 类把代理返回远程对象。代理是 System.Runtime. Remoting.Proxies.__TransparentProxy 类型。这个对象看起来像是真实对象,原因是它提供相同的方法。透明代理使用真实代理把消息发送给信道:
[csharp] view plain copy
 
  1. using RemoteHello;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Runtime.Remoting.Channels;  
  6. using System.Runtime.Remoting.Channels.Tcp;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9.   
  10. namespace HelloClient  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Console.WriteLine("Press return after the server is started");  
  17.             Console.ReadLine();  
  18.   
  19.             //在客户端创建TcpServerChannel信道  
  20.             ChannelServices.RegisterChannel(new TcpClientChannel(), true);  
  21.   
  22.             //GetObject()它调用Remoting Services.Connect()方法以 返回远程对象的代理对象。第一个参数指定远程对象的类型。第二个参数是远程对象的URL。  
  23.             Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");  
  24.            // Hello obj = (Hello)RemotingServices.Connect(typeof(Hello),"tcp://localhost:8086/Hi");  
  25.   
  26.             if (obj == null)  
  27.             {  
  28.                 Console.WriteLine("could not locate server");  
  29.                 return;  
  30.             }  
  31.             for (int i = 0; i < 5; i++)  
  32.             {  
  33.                 Console.WriteLine(obj.Greeting("Stephanie"));  
  34.             }  
  35.             Console.ReadLine();  
  36.         }  
  37.     }  
  38. }  
现在可以启动服务器和客户端。在客户端控制台中, Hello Stephanie 文本会出现 5 次。在服务器控制台窗口中,会看到如下所示的输出结果。因为选择的是 WellKnownObjectMode.SingleCall 激活模式,所以为每一个方法调用创建一个新的实例。
Press return to exit
Constructor called
Greeting called
Constructor called
Greeting called
Constructor called
Greeting called
Constructor called
Greeting called
Constructor called
Greeting called

 

Remoting更多参考: 细细品味C#——.Net Remoting专题(文章下载)

using System;
using System.Collections.Generic;
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace RemoteHello  
{  
    public class Hello:System.MarshalByRefObject   
    {  
        public Hello ()  
        {  
            Console.WriteLine("Constructor called");  
        }  
        public string Greeting(string name)  
        {  
            Console.WriteLine("Greeting called");  
            return "Hello," + name;  
        }  
    }  
}  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Runtime.Remoting;  
using System.Runtime.Remoting.Channels;  
using System.Runtime.Remoting.Channels.Tcp;  
using RemoteHello;  
  
namespace HelloServer  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            //在服务器端创建TcpServerChannel信道  
            var channel = new TcpServerChannel(8086);  
             
            //注册该信道,使之可用于远程对象。  
            ChannelServices.RegisterChannel(channel, true);  
  
            //用于为服务器激活的对象注册远程对象类型,(把知名的远程对象类型注册为 RemotingServices)  
            //第一个参数是 typeof(Hello),它指定远程对象的类型。第二个参数 Hi 是远程对象的 URI, 客户端访问远程对象时要使用这个 URI。 最后一个参数是远程对象的模式。             
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);  
  
            Console.WriteLine("Press return to exit");  
            Console.ReadLine();  
        }  
    }  
} 
using RemoteHello;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Runtime.Remoting.Channels;  
using System.Runtime.Remoting.Channels.Tcp;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace HelloClient  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Console.WriteLine("Press return after the server is started");  
            Console.ReadLine();  
  
            //在客户端创建TcpServerChannel信道  
            ChannelServices.RegisterChannel(new TcpClientChannel(), true);  
  
            //GetObject()它调用Remoting Services.Connect()方法以 返回远程对象的代理对象。第一个参数指定远程对象的类型。第二个参数是远程对象的URL。  
            Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");  
           // Hello obj = (Hello)RemotingServices.Connect(typeof(Hello),"tcp://localhost:8086/Hi");  
  
            if (obj == null)  
            {  
                Console.WriteLine("could not locate server");  
                return;  
            }  
            for (int i = 0; i < 5; i++)  
            {  
                Console.WriteLine(obj.Greeting("Stephanie"));  
            }  
            Console.ReadLine();  
        }  
    }  
}

 

 

 

posted @ 2018-02-07 14:02  sky20080101  阅读(91)  评论(0)    收藏  举报