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

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

微軟的遠程處理框架.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一级毛片 | 三级黄色免费片 | 日韩欧美在线免费观看 | 欧美无毛 | 特黄毛片| 日韩精品一区二区三区免费视频 | 亚洲图区欧美 | 深夜爽爽福利gif在线观看 | 亚洲综合色区中文字幕 | 伊人精品久久久大香线蕉99 | 添人人躁日日躁夜夜躁夜夜揉 | 亚洲婷婷丁香 | 日本不卡高清视频 | 日本高清视频在线www色下载 | 日韩爱爱片 | 欧美在线免费观看视频 | 四虎精品永久在线网址 | 日日插夜夜爽 | 青青青国产精品视频 | 婷婷影院在线观看 | 日本成人高清视频 | 亚洲最大毛片 | 日本综合在线 | 一级女人毛片人一女人 | 欧美无毛 | 午夜性爽快免费视频播放 | 日韩在线色 | 欧美专区亚洲专区 |