国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

微軟的遠程處理框架.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(&quot;reverse({0})&quot;, text);<br>
<br>
     string rev = &quot;&quot;;<br>
<br>
     for (int i=text.length-1; i&gt;=0; i--)<br>
     {<br>
      rev += text[i];<br>
      }<br>
<br>
     console.writeline(&quot;returning : {0}&quot;, 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>
         &quot;server&quot;, // <font color=#ff0000>assembly name</font><br>
         &quot;remotingsample.reverser&quot;, // <font color=#ff0000>full type name</font><br>
         &quot;reverser.soap&quot;, 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(&quot;server.exe&quot;);<br>
     console.writeline(&quot;press enter to stop server...&quot;);<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>
         &quot;http://localhost:8000/reverser.soap&quot; file://<font color=#ff0000> uri</font><br>
         );<br>
      console.writeline(&quot;client.exe&quot;);<br>
      console.writeline(rev.reverse(&quot;hello, world!&quot;));<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 = &quot;david&quot;;<br>
   public string lastname = &quot;findley&quot;;<br>
   private int age = 29;<br>
  }<br>
<br>
  public class theapp<br>
  {<br>
   public static void main()<br>
   {<br>
    stream stream = file.create(&quot;example.xml&quot;);<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, &quot;this is a string&quot;);<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=&quot;ref-1&quot;><br>
  <m_value>5</m_value><br>
  </xsd:int><br>
  </soap-env:body><br>
<br>
  <soap-env:body><br>
  <soap-enc:string id=&quot;ref-1&quot;>this is a string</soap-enc:string><br>
  </soap-env:body><br>
<br>
  <soap-env:body><br>
  <a1:person id=&quot;ref-1&quot;><br>
  <firstname id=&quot;ref-3&quot;>david</firstname><br>
  <lastname id=&quot;ref-4&quot;>findley</lastname><br>
  <age>29</age><br>
  </a1:person><br>
  </soap-env:body><br>
<br>
你可以看出,它可以串行化基本值類和對象。httpchannel使用soap格式器在客戶和服務器之間傳送數據。<br>
<br>
  總的來說,格式器可以格式和保持值或者對象的數據。channel傳送和接收數據。通過channel和格式器的協同工作,我們將可以使用任何的網絡和協議來連接兩個應用。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌拉特前旗| 织金县| 屏东县| 容城县| 兖州市| 荣昌县| 瓦房店市| 桓仁| 襄城县| 报价| 根河市| 齐齐哈尔市| 潍坊市| 金坛市| 延边| 襄城县| 正阳县| 房产| 云霄县| 台东市| 彩票| 娱乐| 海原县| 行唐县| 同德县| 南投市| 泰顺县| 开原市| 铜山县| 洛宁县| 蒙城县| 安远县| 萝北县| 北宁市| 赤城县| 民和| 博湖县| 寿宁县| 冕宁县| 汉川市| 镇坪县|