微軟的遠程處理框架.NET Remoting(轉天極網)之二
2024-07-10 12:59:57
供稿:網友
 
 以下我們將舉一個使用channel的例子。在這個例子中,我們將可以看到使用http channel把兩個應用<br>
連接在一起是如此的簡單。以下的服務器應用提供了一個服務,可將一個字符串的字母順序反轉。<br>
<br>
  server.cs using system;<br>
  using system.io;<br>
  using system.runtime.remoting;<br>
  using system.runtime.remoting.channels.http;<br>
<br>
  namespace remotingsample <br>
  {<br>
   public class reverser : marshalbyrefobject<br>
   {<br>
    public string reverse(string text)<br>
    {<br>
     console.writeline("reverse({0})", text);<br>
<br>
     string rev = "";<br>
<br>
     for (int i=text.length-1; i>=0; i--)<br>
     {<br>
      rev += text[i];<br>
      }<br>
<br>
     console.writeline("returning : {0}", rev);<br>
<br>
     return rev;<br>
    }<br>
   }<br>
<br>
   public class theapp<br>
   {<br>
    public static void main()<br>
    {<br>
     file://<font color=#ff0000> create a new http channel that</font><br>
     // <font color=#ff0000>listens on port 8000</font><br>
     httpchannel channel = new httpchannel(8000);<br>
<br>
     // <font color=#ff0000>register the channel with the runtime</font><br>
     channelservices.registerchannel(channel);<br>
<br>
     // <font color=#ff0000>expose the reverser object from this server</font><br>
     remotingservices.registerwellknowntype(<br>
         "server", // <font color=#ff0000>assembly name</font><br>
         "remotingsample.reverser", // <font color=#ff0000>full type name</font><br>
         "reverser.soap", file://<font color=#ff0000> uri</font><br>
         wellknownobjectmode.singleton // <font color=#ff0000>instancing mode</font><br>
      );<br>
<br>
     // <font color=#ff0000>keep the server running until</font><br>
     // <font color=#ff0000>the user presses enter</font><br>
     console.writeline("server.exe");<br>
     console.writeline("press enter to stop server...");<br>
     console.readline();<br>
    }<br>
   }<br>
  }<br>
<br>
  現在我們已經擁有了一個字符反向服務,以下我們將建立一個客戶應用來使用這個服務:<br>
<br>
   client.cs using system;<br>
   using system.runtime.remoting;<br>
   using system.runtime.remoting.channels.http;<br>
   using remotingsample; // reference the server<br>
<br>
   public class theapp<br>
    {<br>
     public static void main()<br>
     {<br>
      // <font color=#ff0000>create and register a channel</font><br>
      // <font color=#ff0000>to comunicate to the server.</font><br>
      // <font color=#ff0000>the client will use port 8001</font><br>
      // <font color=#ff0000>to listen for callbacks</font><br>
      httpchannel channel = new httpchannel(8001);<br>
      channelservices.registerchannel(channel);<br>
<br>
      // <font color=#ff0000>create an instance on the remote server</font><br>
      // <font color=#ff0000>and call a method remotely</font><br>
      reverser rev = (reverser)activator.getobject(<br>
         typeof(reverser), // <font color=#ff0000>type to create</font><br>
         "http://localhost:8000/reverser.soap" file://<font color=#ff0000> uri</font><br>
         );<br>
      console.writeline("client.exe");<br>
      console.writeline(rev.reverse("hello, world!"));<br>
     }<br>
    }<br>
<br>
<br>
<img src=http://www.163design.net/n/b/"http://www.yesky.com/34670816/jt-2001-4-4-1.jpg">       <img src=http://www.163design.net/n/b/"http://www.yesky.com/34670816/jt-2001-4-4-2.jpg"><br>
************圖一               圖二*******************<br>
<br>
看,通過遠程.net將兩個應用連接在一起是多么的簡單。當服務端和客戶端程序放在兩臺不同的機器時,我們可以令兩個程序都運行在80端口。這樣遠程的調用就可通過一個防火墻。你也可將httpchannel改為一個tcpchannel試一下。<br>
<br>
  你要注意到,客戶端是通過“reverser.soap”來標識它想連接的對象的。這個名字與服務器代碼中registerwellknowntype的uri參數符合。“.soap”的擴展是不必要的。uri可以是任何的字符串,只要它能唯一標識服務器的對象就可以了。“.soap”的擴展只是用來提醒我們http channel是使用soap來格式化信息的。<br>
<br>
  在上面有關channel的例子中,你可能會產生這樣的疑問:參數是如何跨網絡傳送,返回值又是如何送回的呢?答案是,在參數被跨網絡傳送之前,他們必須經過串行化處理。對于需要傳送的所有對象或者結構,都要經過這樣的處理。串行化的處理很簡單,只是以連續字節的方式建立變量或者對象中的數據的一個持續拷貝。將這些字節還原為一個值或者對象實例的處理被稱為反串行化。<br>
<br>
  那么參數是如何串行化的呢?遠程.net架構為我們提供了一個稱為格式器(formatters)的對象集。格式器可將一個對象變成是一個特定的持續數據格式,也可以將該它還原回來。.net為我們提供了兩種格式器:<br>
<br>
  system.runtime.serialization.formatters.binary <br>
  system.runtime.serialization.formatters.soap <br>
<br>
binary(二進制)格式器是最簡單的。它只是將數據直接轉換為一個字節流。soap格式器使用一個xml來保持一個對象數據。要知道soap更詳細的信息,可到http://www.soapwebservices.com。<br>
<br>
以下我們舉一個有關格式器的簡單例子。我們將使用soap格式器,由于它使用的是xml,我們可以很容易地讀出串行化的數據。<br>
<br>
  soap.cs using system;<br>
  using system.io;<br>
  using system.runtime.serialization.formatters.soap;<br>
<br>
  public class person<br>
  {<br>
   public string firstname = "david";<br>
   public string lastname = "findley";<br>
   private int age = 29;<br>
  }<br>
<br>
  public class theapp<br>
  {<br>
   public static void main()<br>
   {<br>
    stream stream = file.create("example.xml");<br>
    soapformatter formatter = new soapformatter();<br>
    person p = new person();<br>
<br>
    // <font color=#ff0000>persist an integer</font><br>
    formatter.serialize(stream, 5);<br>
<br>
    file://<font color=#ff0000> persist a string</font><br>
    formatter.serialize(stream, "this is a string");<br>
<br>
    // <font color=#ff0000>persist an object</font><br>
    formatter.serialize(stream, p);<br>
<br>
    stream.close();<br>
   }<br>
  }<br>
<br>
  對于每個串行化的調用,example.xml的內容將有三個不同的部分:<br>
<br>
  example.xml <br>
<br>
  <soap-env:body><br>
  <xsd:int id="ref-1"><br>
  <m_value>5</m_value><br>
  </xsd:int><br>
  </soap-env:body><br>
<br>
  <soap-env:body><br>
  <soap-enc:string id="ref-1">this is a string</soap-enc:string><br>
  </soap-env:body><br>
<br>
  <soap-env:body><br>
  <a1:person id="ref-1"><br>
  <firstname id="ref-3">david</firstname><br>
  <lastname id="ref-4">findley</lastname><br>
  <age>29</age><br>
  </a1:person><br>
  </soap-env:body><br>
<br>
你可以看出,它可以串行化基本值類和對象。httpchannel使用soap格式器在客戶和服務器之間傳送數據。<br>
<br>
  總的來說,格式器可以格式和保持值或者對象的數據。channel傳送和接收數據。通過channel和格式器的協同工作,我們將可以使用任何的網絡和協議來連接兩個應用。