首页 > 开发 > .Net > 正文

微软的远程处理框架.NET Remoting(转天极网)之二

2020-02-03 15:53:49
字体:
来源:转载
供稿:网友
 以下我们将举一个使用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和格式器的协同工作,我们将可以使用任何的网络和协议来连接两个应用。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表