| 域名空间 下载中心 社区论坛 信息公告 MY小屋 |
![]() |
联系我们 设为首页 加入收藏 |
|
首页 | 新闻资讯 | 编程开发 | 网页设计 | 图形图象 | 网络媒体 | 网站模板 | 数 据 库 | 投稿 论坛 | 操作系统 | 系统优化 | 网络安全 | 黑客技术 | 硬件学堂 | 硬件报价 | 服 务 器 | 地图 专题 | 应用软件 | 聊天通讯 | Q Q 专栏 | 建站经验 | 在线工具 | 站长Club | 注 册 表 | 旧版 社会 | 游戏娱乐 | 设计欣赏 | 疑难解答 | 社区论坛 | 韩国素材 | 素材图库 | 广告服务 | 服务 |
| 新版上线![旧版] | |||||
注:打开慢时请稍等
微软的远程处理框架.NET Remoting - 2http://www.iyit.net 日期:2006-6-12 1:58:58 来源: 点击: |
连接在一起是如此的简单。以下的服务器应用提供了一个服务,可将一个字符串的字母顺序反转。 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和格式器的协同工作,我们将可以使用任何的网络和协议来连接两个应用。 编辑:黑鹰 [发送给好友] [打印本页] [关闭窗口] [返回顶部] 上一篇:黑客写出了针对.Net平台的反编译器 下一篇:微软的远程处理框架.NET Remoting - 1 转载请注明来源:www.iyit.net 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。 |
| 最新更新 | 热点排行 | 推荐新闻 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 友情链接 | ||||||
| 设置首 页 - 版权声明 - 广告服务 - 关于我们 - 联系我们 - 友情连接 |
| |||||||