zoukankan      html  css  js  c++  java
  • NET Remoting 最简单示例

    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,查看运行结果。
  • 相关阅读:
    asp.net(.net 4.0)+ json 分页
    在两张表(A表和B表)里面找出A中不存在B表的记录
    linq to sql 的List<Table> 数据表缓存
    linq 并发冲突概念
    阿里RocketMq节约成本
    阿里巴巴java手册异常日志
    阿里巴巴java手册安全规约
    阿里巴巴java手册单元测试
    Spring boot自定义starter
    MongoDB权限
  • 原文地址:https://www.cnblogs.com/skyay/p/5687636.html
Copyright © 2011-2022 走看看