首页
技术库|站长工具|技术手册|字体库|知识点词汇表| 联系我们|
打开本页的html静态页面
 

主菜单

文章分类

.: .Net技术 .: .Net分析与设计 .: .net remoting的示例代码

  • 全文内容
  • 发表评论
  • 文章点评
  • 文章附件
  • Email文章
  • 打印文章

.net remoting的示例代码

点击次数:1031 创建日期:3-15-2008 录入:cn-web.com 字体:[ ] 点评:


此示例主要演示了net remoting,其中包含一个服务器程序Server.exe和一个客户端程序CAOClient.exe。客户端程序会通过http channel调用服务器端RemoteType.dll的对象和方法。
 
服务器端的代码文件由下图所述:
 
Server.cs源代码 :
using System;
using System.Runtime.Remoting;
public class Server{
public static void Main(string[] Args){
// Load the configuration file
RemotingConfiguration.Configure("server.exe.config");
Console.WriteLine("The server is listening. Press Enter to exit....");
Console.ReadLine();
Console.WriteLine("GC'ing.");
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

Server.exe.config源代码:

<SYSTEM.RUNTIME.REMOTING>
<APPLICATION>
<SERVICE>
<ACTIVATED type="ClientActivatedType, RemoteType">
</SERVICE>
<CHANNELS>
<CHANNEL ref="http" port="8088">
</CHANNELS>
</APPLICATION>
</SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>
RemoteType.cs源代码:
using System;
using System.Runtime.Remoting.Lifetime;
using System.Security.Principal;
public class ClientActivatedType : MarshalByRefObject{
private int i;
// override the lease settings for this object
public override Object InitializeLifetimeService(){
return null;
}
public string RemoteMethod(){
// announce to the server that we've been called.
Console.WriteLine("ClientActivatedType.RemoteMethod called.");
// report our client identity name
i=this.GetHashCode();
return "RemoteMethod called. " + i;
}
public string RemoteMethod1(){
return "RemoteMethod1 called. " + i;
}
}
客户端代码文件由下图所示:
 

CAOClient.cs源代码
 using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
public class Client{
public static void Main(string[] Args){
// Load the configuration file
RemotingConfiguration.Configure("CAOclient.exe.config");
ClientActivatedType CAObject = new ClientActivatedType();
Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod());
Console.WriteLine("Client-activated object: " + CAObject.RemoteMethod1());
Console.WriteLine("Press Enter to end the client application domain.");
Console.ReadLine();
}
}
 
CAOClient.exe.config源代码:
 <CONFIGURATION>
<SYSTEM.RUNTIME.REMOTING>
<APPLICATION>
<CLIENT url="http://localhost:8088">
<ACTIVATED type="ClientActivatedType, RemoteType">
</CLIENT>
<CHANNELS>
<CHANNEL ref="http" port="0">
</CHANNELS>
</APPLICATION>
</SYSTEM.RUNTIME.REMOTING>
</CONFIGURATION>

OK,编译以上代码文件:
 
使用“Visual Studio .net Command Prompt="分别编译上述文件:
csc /target:library RemoteType.cs
csc Server.cs
csc –reference:RemoteType.dll CAOClient.cs

您会看到三个输出文件:RemoteType.dll, Server.exe 和 CAOClient.exe。
运行Remoting程序
在命令行方式下启动:Server.exe
在命令行方式下启动:CAOClient.exe
 

请文明参与讨论,禁止漫骂攻击。
评论总数:0 [ 查看全部 ] 网友评论
此文章还没有任何评论!
(+5分)
(+4分)
(+3分)
(+2分)
(+1分)
此内容无附件
网站地图 - 知识词汇 - 全文检索 - 广告服务 - 帮助中心 - 联系我们
.:www.cn-web.com
网站技术开发联盟之WEB开发技术知识库
联系人:老韩(QQ:5679551)
晋ICP备07003487号