zoukankan      html  css  js  c++  java
  • .NET Remoting 实例

    http://www.microsoft.com/china/technet/security/guidance/secmod11.mspx
     .NET Remoting 安全性
    *******************************************************************************************
    http://www.iiiedu.org.tw/knowledge/knowledge20030430_2.htm
    微軟以往使用COM/DCOM的技術來處理分散式系統架構,透過Client端的Proxy代理程式來呼叫遠端Server機器上的物件。.NET Framework則使用.NET Remoting或Web Services技術來實作分散式處理的工作概念;在此針對.NET Remoting的設計架構做一個初步的簡介。
    .NET Framework提供了多種的機制來支援Remoting,如:
    .利用Channel來負責訊息的傳送與接收。
    .利用Formatter來負責在訊息要透過channel傳送出去之前,先將訊息做適當的加密,或於訊息在透過Channel接收進來之後,先將訊息做相對的解密工作。
    .利用Proxy來呼叫遠端的物件執行所要的功能呼叫。
    其關係如下圖所示:

    Channel 和 Formatter
    在遠端物件被使用之前,必須先在Server端註冊好訊息傳送的通道(Channel),這些Channel可透過.NET Remotin configuration file或 ChannelServices物件類別的RegisterChannel方法來註冊。

    在Channel的使用上,.NET Framework支援HTTP、TCP及SMTP等通道。若使用HTTP Channel ,則使用SOAP協定來收送訊息,所有的訊息會被傳送到SOAP Formatter中,被序列化(serialized)成XML的格式,而SOAP所需的headers也會被加入。至於使用TCP Channel者,則使用TCP協定來將訊息傳送到Binary Formatter中,以Binary Stream的方式來將訊息傳送到URI目的地。(URI : Universal Resource Identifier,類似大家所熟悉的URL)。

    Activation and Proxy
    Server-Side Activation
    Server端在Client端要存取Remoting物件時必需在Server端能自動啟始Remoting物件,可使用RemotingConfiguration物件類別的RegisterWellKnownServiceType方法來完成這項工作。

    Client-Side Activation
    Client端要使用遠端物件之前,可使用New 或Activator 物件類別所提供的CreateInstance或GetObject方法來啟動物件並傳回Proxy,以便Client端可透過Proxy來執行叫用遠端物件的方法。
    ***************************************************************************************

    第一步:创建远程的共享库 
    为了说明.NET Remoting 是如何运行的,先创建一个简单的类库,以创建远程的对象。
    依次点击“文件”->“新创建”->“工程”,选择创建一个C# Library,并将其命名为RemoteHello,然后点击OK按钮。这将创建一个.NET Remote客户端和服务器端用来通讯的“共享命令集”。编译创建的工程,就会得到一个DLL文件,并可以在其他的工程中使用它。(右键-〉“生成”与CSC生成的Library有区别吗?不应该有区别的啊!) 
    程序集的名称是RemoteHello.dll,类的名称是Hello, 类Hello是从System.MarshallByRefObject 派生出来的。
    Hello.cs:
    using System;
    using System.Collections.Generic;
    using System.Text;


    namespace RemoteHello
    {
        
        
    public class Hello : System.MarshalByRefObject
        
    {
            
    public Hello()
            
    {
                Console.WriteLine(
    "Constructor called");
            }

            
    ~Hello()
            
    {
                Console.WriteLine(
    "Destructor called");
            }

            
    public string Greeting(string name)
            
    {
                Console.WriteLine(
    "Greeting called");
                
    return "Hello," + name;
            }


        }



    }

    第二步:创建简单的服务器
    创建一个C#控制台应用程序HelloServer 。 为了使用TcpServerChannel类,必须引用
    System.Runtime.Remoting程序集
    ,另外更重要的是,引用上面创建的RemoteHello程序集
    在Main()方法中,用端口号8086创建一个 System.Runtime.Channels.Tcp信道,该信道
    使用System.Runtiem.Remoting.Channels.ChannelServices注册,使之用于远程对象。在远程对象注册之后,使服务器一直处于运行状态,直到按任意键为止:
    HelloServer.cs:
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using RemoteHello;
    namespace  RemoteHello
    {
        
    public  class HelloServer
        
    {
            [STAThread]
            
    public static void Main(string[] args)
            
    {
                
    //System.Runtime.Remoting.Channels.Tcp.TcpServerChannel为远程调用,实现,使用TCP协议,传输消息的服务器信道,8086是信道侦听的端口
                TcpServerChannel channel = new TcpServerChannel(8086);
                
    //System.Runtime.Remoting.Channels.ChannelServices提供帮助进行,远程处理信道注册\解析和URL发现的静态方法,无法继承此类.
               
                ChannelServices.RegisterChannel(channel,
    true);//该方法向信道服务,注册信道

                
    //System.Runtime.Remoting.RemotingConfiguration提供多种,配置远程处理结构,的静态方法
                
    //RegisterWellKnownServiceType方法将在服务器端提供的System.Runtime.Remoting.WellKnownServiceTypeEntry中记录的对象System.Type注册为已知类型
                
    //WellKnownObjectMode.SingleCall正在被注册的已知对象类型的激活方式
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteHello.Hello), "Hi", WellKnownObjectMode.SingleCall);
                
                System.Console.WriteLine(
    "hit to exit");
                System.Console.ReadLine();
            }

        }

    }


    第三步: 创建简单的客户机
    客户机也是一个C#控制台应用程序 HelloClient. 这里也引用了System.Runtime.Remoting程序集,以便使用TcpClientChannel类。 此外,也必须引用RemoteHello程序集。
    在客户机程序中,要创建一个TcpClientChannel对象,这个对象注册在ChannelServices 中。
    对于TcpChannel,使用默认的构造函数,因此可以选择任意一个端口。接下来使用Activator类把代理对象返回给远程对象。
    HelloClient.cs:
    using System;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;


    namespace RemoteHello
    {
        
    /*
         * 客户机也是一个C#控制台应用程序 HelloClient. 这里也引用了System.Runtime.Remoting程序集,以便使用TcpClientChannel类。 
         * 此外,也必须引用RemoteHello程序集。
         * 在客户机程序中,要创建一个TcpClientChannel对象,这个对象注册在ChannelServices 中。
         * 对于TcpChannel,使用默认的构造函数,因此可以选择任意一个端口。接下来使用Activator类把代理对象返回给远程对象。
         
    */

        
    public class HelloClient
        
    {
            [STAThread]
            
    public static void Main(string[] args)
            
    {
                
    //System.Runtime.Remoting.Channels.Tcp.TcpClientChannel//为远程调用实现,使用TCP协议传输消息,的客户端信道
                ChannelServices.RegisterChannel(new TcpClientChannel(),true);
                
    //System.Activator:包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用.无法继承此类.
                Hello obj = (Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8086/Hi");
                
    if (obj == null)
                
    {
                    Console.WriteLine(
    "could not locate server");
                    
    return;
                }

                
    for (int i = 0; i < 5; i++)
                
    {
                    
    //调用远程对象obj的Greeting方法,此时hello位于RemoteHello.dll中,并不是HelloServer.exe中,
                    
    //但是在ildasm显示中有IL_0014:  ldtoken    [RemoteHello]RemoteHello.Hello这条指令指示当HelloServer.exe运行时,要"加载"RemoteHello
                    
    //所以,此处或者可以说是:"调用HelloServer中的Greeting方法"
                    Console.WriteLine(obj.Greeting("Christian"));
                }

            }

        }

    }



    问题是:
    当打开服务器和客户机程序Hello时,Christian在客户控制中会出现5次,。。。
    我怎么打开服务器程序Hello?csc HelloServer.cs 然后运行生成的HelloServer.exe对吗?

    这个错误原因是:csc /r:remoteHello.dll helloserver.cs如果remoteHello.dll与helloServer.cs不在同一目录下应该用:csc /r:D:\console\remoteHello\bin\debug\remoteHello.dll helloserver.cs
    将HelloServer.cs 和HelloClient.cs分别编译成 .exe文件后。再“运行”-〉cmd而不是Visual Studio 2005命令行提示,在第一个cmd中运行HelloServer,再启用一个cmd运行HelloClient,必须首先在第一个cmd中运行“服务器端”HelloServer

    谁能将此应用扩张成----〉更有实际意义的有一点价值的情况?????????????????
  • 相关阅读:
    经典算法之猴子吃桃
    VS2008C#Sqlserver2008数据库的连接以及增删改查
    在数组中随机插入数字且不重复
    菲波那切数列
    Js之AJAX
    经典算法之冒泡排序
    《Head First 设计模式》 第一章 设计模式入门
    Redis 的 IO 多路复用,学习研究
    高性能MySQL 第十章 复制 Part2
    高性能MySQL 第十一章 可扩展的MySQL
  • 原文地址:https://www.cnblogs.com/simhare/p/954508.html
Copyright © 2011-2022 走看看