微软的远程处理框架.NET Remoting - 2

termny

termny

2016-01-29 13:49

微软的远程处理框架.NET Remoting - 2,微软的远程处理框架.NET Remoting - 2
以下我们将举一个使用channel的例子。在这个例子中,我们将可以看到使用HTTP channel把两个应用

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

连接在一起是如此的简单。以下的服务器应用提供了一个服务,可将一个字符串的字母顺序反转。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

  Server.cs using System;

  using System.IO;

  using System.Runtime.Remoting;

  using System.Runtime.Remoting.Channels.HTTP;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

  namespace RemotingSample

  {

   public class Reverser : MarshalByRefObject

   {

    public string Reverse(string text)

    {

     Console.WriteLine("Reverse({0})", text);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

     string rev = "";

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

     for (int i=text.Length-1; i=0; i--)

     {

      rev += text[i];

      }

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

     Console.WriteLine("returning : {0}", rev);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

     return rev;

    }

   }

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

   public class TheApp

   {

    public static void Main()

    {

     file:// Create a new HTTP channel that

     // listens on port 8000

     HTTPChannel channel = new HTTPChannel(8000);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

     // Register the channel with the runtime

     ChannelServices.RegisterChannel(channel);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

     // 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

      );

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

     // keep the server running until

     // the user presses enter

     Console.WriteLine("Server.exe");

     Console.WriteLine("Press enter to stop server...");

     Console.ReadLine();

    }

   }

  }

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

  现在我们已经拥有了一个字符反向服务,以下我们将建立一个客户应用来使用这个服务:

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

   Client.cs using System;

   using System.Runtime.Remoting;

   using System.Runtime.Remoting.Channels.HTTP;

   using RemotingSample; // reference the server

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

   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);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

      // 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

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

         );

      Console.WriteLine("Client.exe");

      Console.WriteLine(rev.Reverse("Hello, World!"));

     }

    }

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

看,通过远程.NET将两个应用连接在一起是多么的简单。当服务端和客户端程序放在两台不同的机器时,我们可以令两个程序都运行在80端口。这样远程的调用就可通过一个防火墙。你也可将HTTPChannel改为一个TCPChannel试一下。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

  你要注意到,客户端是通过“Reverser.soap”来标识它想连接的对象的。这个名字与服务器代码中RegisterWellKnownType的URI参数符合。“.soap”的扩展是不必要的。URI可以是任何的字符串,只要它能唯一标识服务器的对象就可以了。“.soap”的扩展只是用来提醒我们HTTP channel是使用soap来格式化信息的。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com)

  在上面有关channel的例子中,你可能会产生这样的疑问:参数是如何跨网络传送,返回值又是如何送回的呢?答案是,在参数被跨网络传送之前,他们必须经过串行化处理。对于需要传送的所有对象或者结构,都要经过这样的处理。串行化的处理很简单,只是以连续字节的方式建立变量或者对象中的数据的一个持续拷贝。将这

展开更多 50%)
分享

猜你喜欢

微软的远程处理框架.NET Remoting - 2

电脑网络
微软的远程处理框架.NET Remoting - 2

微软的远程处理框架.NET Remoting - 1

电脑网络
微软的远程处理框架.NET Remoting - 1

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

XML、Web服务和.NET框架(2)

Web开发
XML、Web服务和.NET框架(2)

基于.NET数字处理程序的框架设计

Web开发
基于.NET数字处理程序的框架设计

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

Microsoft .NET Romoting 框架简介

电脑网络
Microsoft .NET Romoting 框架简介

.NET框架下的自动内存管理

电脑网络
.NET框架下的自动内存管理

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

显示隐藏鼠标

显示隐藏鼠标

程序员眼中的Flash MX2004(2)

程序员眼中的Flash MX2004(2)
下拉加载更多内容 ↓