首页
技术库 |  站长工具 | 技术手册 |  字体库 |  知识点词汇表  |  联系我们 |   



主菜单


站点首页
技术手册
字体库
知识点 词汇表
站长工具
高级搜索
联系我们
站点地图

文章分类



文章内容 

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



此示例主要演示了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
 




隐藏文章属性
文章编号:630
点击次数:785
创建日期:3-15-2008
修改日期:3-15-2008
发布人:laohan
点评:
发送此文
发表评论
打印
添加到收藏夹


评级:




用户评论

此文章还没有任何评论!

网站地图 - 知识词汇 - 全文检索 - 广告服务 - 帮助中心 - 联系我们
.:www.cn-web.com
网站技术开发联盟之WEB开发技术知识库
联系人:老韩(QQ:5679551)
晋ICP备07003487号