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

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

微軟的遠程處理框架.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和格式器的協同工作,我們將可以使用任何的網絡和協議來連接兩個應用。





主站蜘蛛池模板: 午夜剧院免费 | 在线观看亚洲免费视频 | 日本不卡一区二区三区在线观看 | 天天舔天天 | 日本一二三区在线视频 | 午夜亚洲福利 | 欧美一级美片在线观看免费 | 五月天婷婷精品视频 | 色羞羞| 日本欧美视频在线观看 | 午夜黄色毛片 | 一区小说二区另类小说三区图 | 亚洲mv在线观看 | 亚洲天堂中文字幕在线观看 | 欧美一区精品 | 五月激情五月婷婷 | 青草园网站在线观看 | 三级在线观看免播放网站 | 欧美特黄a级高清免费大片 欧美桃色视频 | 社区天堂| 日韩美女在线看免费观看 | 午夜88| 思思久久96热在精品国产免费 | 热久久这里是精品6免费观看 | 日日摸夜夜欧美一区二区 | 天天干天天干天天干天天 | 日本不卡视频在线观看 | 日韩亚 | 日本午夜在线观看 | 日本精品视频网站 | 午夜亚洲精品 | 伊人久久综合 | 亚洲精品乱码久久久久久 | 伊人久久大香线蕉资源 | 在线观看欧美日韩 | 青青草国产免费久久久下载 | 青娱乐国产盛宴 | 日韩视频免费在线 | 日韩中文字幕视频在线 | 青娱乐欧美视频 | 日本高清不卡网站免费 |