.NET Remoting 最简单示例

学习技术知识一个好的方法是先动手,再深入,

给出一个最简单的Remoting程序示例(C#)如下:


Step1:创建类库(DLL)工程RemotingObjects,类Person代码如下:

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace RemotingObjects  
  8. {  
  9.     public interface IPerson  
  10.     {  
  11.         String getName(String name);  
  12.   
  13.     }  
  14.   
  15.     public class Person : MarshalByRefObject, IPerson  
  16.     {  
  17.         public Person()  
  18.         {  
  19.             Console.WriteLine("[Person]:Remoting Object 'Person' is activated.");  
  20.         }  
  21.   
  22.         public String getName(String name)  
  23.         {  
  24.             return name;  
  25.         }  
  26.     }  
  27. }  
Step2:创建控制台工程RemotingServer(添加项目引用RemotingObjects),类Server代码如下:

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Remoting;  
  5. using System.Runtime.Remoting.Channels;  
  6. using System.Runtime.Remoting.Channels.Tcp;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9.   
  10.   
  11. namespace RemotingServer  
  12. {  
  13.     class Server  
  14.     {  
  15.         static void Main(string[] args)  
  16.         {  
  17.             TcpChannel channel = new TcpChannel(8080);  
  18.             ChannelServices.RegisterChannel(channel, false);  
  19.             RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObjects.Person), "RemotingPersonService", WellKnownObjectMode.SingleCall);  
  20.   
  21.             System.Console.WriteLine("Server:Press Enter key to exit");  
  22.             System.Console.ReadLine();  
  23.         }  
  24.     }  
  25. }  

Step3:创建控制台工程RemotingClient(添加项目引用RemotingObjects及必要类库),类Client代码如下:

(PS:正式应用开发,不需要也不应该直接引用RemotingObjects类库,而应该引用相关Remoting类的接口库。)

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using RemotingObjects;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Runtime.Remoting.Channels;  
  6. using System.Runtime.Remoting.Channels.Tcp;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9.   
  10. namespace RemotingClient  
  11. {  
  12.     class Client  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             TcpChannel channel = new TcpChannel();  
  17.             ChannelServices.RegisterChannel(channel, false);  
  18.             IPerson obj = (IPerson)Activator.GetObject(typeof(RemotingObjects.IPerson), "tcp://localhost:8080/RemotingPersonService");  
  19.             if (obj == null)  
  20.             {  
  21.                 Console.WriteLine("Couldn't crate Remoting Object 'Person'.");  
  22.             }  
  23.   
  24.             Console.WriteLine("Please enter your name:");  
  25.             String name = Console.ReadLine();  
  26.             try  
  27.             {  
  28.                 Console.WriteLine(obj.getName(name));  
  29.             }  
  30.             catch (System.Net.Sockets.SocketException e) {  
  31.                 Console.WriteLine(e.ToString());  
  32.             }  
  33.                 Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  
Step4:运行编译出的EXE:RemotingServer.exe和RemotingClient.exe,查看运行结果。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值