.Net Remoting 技术示例

1、创建可远程的共享库(提供类型)。

为了说明.NET Remoting 是如何运行的,先创建一个简单的类库,以创建远程的对象。
依次点击“文件”->“新创建”->“工程”,选择创建一个C# Library,并将其命名为RemoteHello,然后点击OK按钮。这将创建一个.NET Remote客户端和服务器端用来通讯的“共享命令集”。编译创建的工程,就会得到一个DLL文件,并可以在其他的工程中使用它。(右键-〉“生成”与CSC生成的Library有区别吗?不应该有区别的啊!) 
程序集的名称是RemoteHello.dll,类的名称是Hello, 类Hello是从System.MarshallByRefObject 派生出来的。
Hello.cs:
  using System;
using System.Collections.Generic;
using System.Text;


namespace RemoteHello                                                    //可远程使用的类
{
    
    
public class Hello : System.MarshalByRefObject      //区别于普通类之处 
    
{
        
public Hello()
       
{
            Console.WriteLine(
"Constructor called");
        }

        
~Hello()
        
{
            Console.WriteLine(
"Destructor called");
        }

        
public string Greeting(string name)
        
{
            Console.WriteLine(
"Greeting called");
            
return "Hello," + name;
        }


    }


}

2、服务器端程序。

创建一个C#控制台应用程序HelloServer 。 为了使用TcpServerChannel类,必须引用
System.Runtime.Remoting程序集
,另外更重要的是,引用上面创建的RemoteHello程序集
在Main()方法中,用端口号8086创建一个 System.Runtime.Channels.Tcp信道,该信道
使用System.Runtiem.Remoting.Channels.ChannelServices注册,使之用于远程对象。在远程对象注册之后,使服务器一直处于运行状态,直到按任意键为止:
HelloServer.cs:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteHello;
namespace  RemoteHello
{
    
public  class HelloServer
    
{
        [STAThread]
        
public static void Main(string[] args)
        
{
            
//System.Runtime.Remoting.Channels.Tcp.TcpServerChannel为远程调用,实现,使用TCP协议,传输消息的服务器信道,8086是信道侦听的端口
            TcpServerChannel channel = new TcpServerChannel(8086);
            
//System.Runtime.Remoting.Channels.ChannelServices提供帮助进行,远程处理信道注册\解析和URL发现的静态方法,无法继承此类.
           
            ChannelServices.RegisterChannel(channel,
true);//该方法向信道服务,注册信道

            
//System.Runtime.Remoting.RemotingConfiguration提供多种,配置远程处理结构,的静态方法
            
//RegisterWellKnownServiceType方法将在服务器端提供的System.Runtime.Remoting.WellKnownServiceTypeEntry中记录的对象System.Type注册为已知类型
            
//WellKnownObjectMode.SingleCall正在被注册的已知对象类型的激活方式
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteHello.Hello), "Hi", WellKnownObjectMode.SingleCall);
            
            System.Console.WriteLine(
"hit to exit");
            System.Console.ReadLine();
        }

    }

}

3、客户端程序。

客户机也是一个C#控制台应用程序 HelloClient. 这里也引用了System.Runtime.Remoting程序集,以便使用TcpClientChannel类。 此外,也必须引用RemoteHello程序集。
在客户机程序中,要创建一个TcpClientChannel对象,这个对象注册在ChannelServices 中。
对于TcpChannel,使用默认的构造函数,因此可以选择任意一个端口。接下来使用Activator类把代理对象返回给远程对象。
HelloClient.cs:
using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;


namespace RemoteHello
{
    
/*
     * 客户机也是一个C#控制台应用程序 HelloClient. 这里也引用了System.Runtime.Remoting程序集,以便使用TcpClientChannel类。 
     * 此外,也必须引用RemoteHello程序集。
     * 在客户机程序中,要创建一个TcpClientChannel对象,这个对象注册在ChannelServices 中。
     * 对于TcpChannel,使用默认的构造函数,因此可以选择任意一个端口。接下来使用Activator类把代理对象返回给远程对象。
     
*/

    
public class HelloClient
    
{
        [STAThread]
        
public static void Main(string[] args)
        
{
            
//System.Runtime.Remoting.Channels.Tcp.TcpClientChannel//为远程调用实现,使用TCP协议传输消息,的客户端信道
            ChannelServices.RegisterChannel(new TcpClientChannel(),true);
            
//System.Activator:包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用.无法继承此类.
            Hello obj = (Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8086/Hi");
            
if (obj == null)
            
{
                Console.WriteLine(
"could not locate server");
                
return;
            }

            
for (int i = 0; i < 5; i++)
            
{
                
//调用远程对象obj的Greeting方法,此时hello位于RemoteHello.dll中,并不是HelloServer.exe中,
                
//但是在ildasm显示中有IL_0014:  ldtoken    [RemoteHello]RemoteHello.Hello这条指令指示当HelloServer.exe运行时,要"加载"RemoteHello
                
//所以,此处或者可以说是:"调用HelloServer中的Greeting方法"
                Console.WriteLine(obj.Greeting("Christian"));
            }

        }

    }

}

运行方法:

打开一个cmd,运行“服务器端”HelloServer;

再打开一个cmd,运行“客户端”HelloClient。

如果HelloClient在网络中另外一台计算机上运行,HelloClient中localhost应换为HelloServer所在机器名(

Remotable type 注册在该机器上)。

我们同样可以在HelloServer 所在机器上通过cmd,netstat -a 命令来查看所有端口。

可以看到我们的tcp 8086端口。

 

 

 

 

 

 

 

 

 

 


原文地址:https://www.cnblogs.com/MayGarden/p/1640300.html