六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

微軟的遠程處理框架.NET Remoting - 2

[摘要]以下我們將舉一個使用channel的例子。在這個例子中,我們將可以看到使用HTTP channel把兩個應用 連接在一起是如此的簡單。以下的服務器應用提供了一個服務,可將一個字符串的字母順序反轉。   Server.cs using System;   using System.IO;   usi...
以下我們將舉一個使用channel的例子。在這個例子中,我們將可以看到使用HTTP channel把兩個應用

連接在一起是如此的簡單。以下的服務器應用提供了一個服務,可將一個字符串的字母順序反轉。

  Server.cs using System;

  using System.IO;

  using System.Runtime.Remoting;

  using System.Runtime.Remoting.Channels.HTTP;

  namespace RemotingSample

  {

   public class Reverser : MarshalByRefObject

   {

    public string Reverse(string text)

    {

     Console.WriteLine("Reverse({0})", text);

     string rev = "";

     for (int i=text.Length-1; i>=0; i--)

     {

      rev += text[i];

      }

     Console.WriteLine("returning : {0}", rev);

     return rev;

    }

   }

   public class TheApp

   {

    public static void Main()

    {

     file:// Create a new HTTP channel that

     // listens on port 8000

     HTTPChannel channel = new HTTPChannel(8000);

     // Register the channel with the runtime

     ChannelServices.RegisterChannel(channel);

     // Expose the Reverser object from this server

     RemotingServices.RegisterWellKnownType(

         "server", // assembly name

         "RemotingSample.Reverser", // full type name

         "Reverser.soap", file:// URI

         WellKnownObjectMode.Singleton // instancing mode

      );

     // keep the server running until

     // the user presses enter

     Console.WriteLine("Server.exe");

     Console.WriteLine("Press enter to stop server...");

     Console.ReadLine();

    }

   }

  }

  現在我們已經擁有了一個字符反向服務,以下我們將建立一個客戶應用來使用這個服務:

   Client.cs using System;

   using System.Runtime.Remoting;

   using System.Runtime.Remoting.Channels.HTTP;

   using RemotingSample; // reference the server

   public class TheApp

    {

     public static void Main()

     {

      // Create and register a channel

      // to comunicate to the server.

      // The client will use port 8001

      // to listen for callbacks

      HTTPChannel channel = new HTTPChannel(8001);

      ChannelServices.RegisterChannel(channel);

      // create an instance on the remote server

      // and call a method remotely

      Reverser rev = (Reverser)Activator.GetObject(

         typeof(Reverser), // type to create

         "http://localhost:8000/Reverser.soap" file:// URI

         );

      Console.WriteLine("Client.exe");

      Console.WriteLine(rev.Reverse("Hello, World!"));

     }

    }

看,通過遠程.NET將兩個應用連接在一起是多么的簡單。當服務端和客戶端程序放在兩臺不同的機器時,我們可以令兩個程序都運行在80端口。這樣遠程的調用就可通過一個防火墻。你也可將HTTPChannel改為一個TCPChannel試一下。

  你要注意到,客戶端是通過“Reverser.soap”來標識它想連接的對象的。這個名字與服務器代碼中RegisterWellKnownType的URI參數符合。“.soap”的擴展是不必要的。URI可以是任何的字符串,只要它能唯一標識服務器的對象就可以了。“.soap”的擴展只是用來提醒我們HTTP channel是使用soap來格式化信息的。

  在上面有關channel的例子中,你可能會產生這樣的疑問:參數是如何跨網絡傳送,返回值又是如何送回的呢?答案是,在參數被跨網絡傳送之前,他們必須經過串行化處理。對于需要傳送的所有對象或者結構,都要經過這樣的處理。串行化的處理很簡單,只是以連續字節的方式建立變量或者對象中的數據的一個持續拷貝。將這些字節還原為一個值或者對象實例的處理被稱為反串行化。

  那么參數是如何串行化的呢?遠程.NET架構為我們提供了一個稱為格式器(formatters)的對象集。格式器可將一個對象變成是一個特定的持續數據格式,也可以將該它還原回來。.NET為我們提供了兩種格式器:

  System.Runtime.Serialization.Formatters.Binary

  System.Runtime.Serialization.Formatters.SOAP

binary(二進制)格式器是最簡單的。它只是將數據直接轉換為一個字節流。SOAP格式器使用一個XML來保持一個對象數據。要知道SOAP更詳細的信息,可到http://www.soapwebservices.com。

以下我們舉一個有關格式器的簡單例子。我們將使用SOAP格式器,由于它使用的是XML,我們可以很容易地讀出串行化的數據。

  Soap.cs using System;

  using System.IO;

  using System.Runtime.Serialization.Formatters.Soap;

  public class Person

  {

   public string FirstName = "David";

   public string LastName = "Findley";

   private int Age = 29;

  }

  public class TheApp

  {

   public static void Main()

   {

    Stream stream = File.Create("example.xml");

    SoapFormatter formatter = new SoapFormatter();

    Person p = new Person();

    // persist an integer

    formatter.Serialize(stream, 5);

    file:// persist a string

    formatter.Serialize(stream, "This is a string");

    // persist an object

    formatter.Serialize(stream, p);

    stream.Close();

   }

  }

  對于每個串行化的調用,example.xml的內容將有三個不同的部分:

  Example.xml

  <SOAP-ENV:Body>

  <xsd:int id="ref-1">

  <m_value>5</m_value>

  </xsd:int>

  </SOAP-ENV:Body>

  <SOAP-ENV:Body>

  <SOAP-ENC:string id="ref-1">This is a string</SOAP-ENC:string>

  </SOAP-ENV:Body>

  <SOAP-ENV:Body>

  <a1:Person id="ref-1">

  <FirstName id="ref-3">David</FirstName>

  <LastName id="ref-4">Findley</LastName>

  <Age>29</Age>

  </a1:Person>

  </SOAP-ENV:Body>

你可以看出,它可以串行化基本值類和對象。HTTPChannel使用SOAP格式器在客戶和服務器之間傳送數據。

  總的來說,格式器可以格式和保持值或者對象的數據。Channel傳送和接收數據。通過channel和格式器的協同工作,我們將可以使用任何的網絡和協議來連接兩個應用。





主站蜘蛛池模板: 日本国产在线视频 | 亚洲a级片在线观看 | 日韩欧美黄色 | 一级欧美在线的视频 | 亚洲欧美成人完整版在线 | 日韩一级生活片 | 亚洲成av人片在线观看天堂无码 | 日本在线高清视频 | 青青青在线视频播放 | 日本人视频网站 | 日本三级理论 | 午夜片在线观看 | 午夜剧院免费 | 亚洲欧美日韩国产综合久 | 中文字幕在线视频不卡 | 婷婷久月| 手机看片国产福利 | 四虎a456tncom| 日韩最新中文字幕 | 四虎影视在永久在线观看 2019 | 四虎影院一区二区 | 亚洲一区二区黄色 | 伊人网址| 亚洲精品综合在线 | 色综合久久中文字幕网 | 亚洲欧美在线精品 | 欧美又黄又嫩大片a级 | 午夜tv影院 | 五月婷婷伊人 | 先锋资源久久 | 永久在线观看www免费视频 | 日本中文字幕视频 | 亚洲欧美日韩不卡一区二区三区 | 色综合久久久久久久久五月 | 四虎影院久久 | 午夜亚洲一区 | 中文字幕在线色 | 亚洲欧美天堂 | 五月天婷婷精品视频 | 香蕉香蕉国产片一级一级毛片 | 日本一区免费看 |