remoteing2
生活随笔
收集整理的這篇文章主要介紹了
remoteing2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
此示例主要演示了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();
}
}
<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;
}
} 客戶端代碼文件由下圖所示:
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
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
總結
以上是生活随笔為你收集整理的remoteing2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 应用程序的主入口点应用程序的主入口点应用
- 下一篇: dll文件的c++制作dll文件的c++