时间: 2020-11-21|43次围观|0 条评论

实现已知对象:
1.构建服务器端:
(1)添加对System.Runtime.Remoting.dll
(2)实现一个派生自MarshalByRefObject的类
(3)选择一种可用的通道,TCP,HTTP,然后用ChannelServices.RegisterChannel()方法注册此通道
(4)用RemotingConfiguration.RegisterWellKnownServiceType()方法把这个类注册为已知对象
(5)保持服务器处于激活态
首先是继承了MarshalByRefObject的类:

.NET Remoting 编程一插图using
 System;
.NET Remoting 编程一插图
.NET Remoting 编程一插图
namespace

 MathLibrary
.NET Remoting 编程一插图1.NET Remoting 编程一插图2
.NET Remoting 编程一插图3

{
.NET Remoting 编程一插图4.NET Remoting 编程一插图5    
/**/
/// <summary>
.NET Remoting 编程一插图6    
///
 SimpleMath 的摘要说明。
.NET Remoting 编程一插图7    
/// </summary>

.NET Remoting 编程一插图6    public class
 SimpleMath:MarshalByRefObject
.NET Remoting 编程一插图4.NET Remoting 编程一插图5    
.NET Remoting 编程一插图3
{
.NET Remoting 编程一插图6        
public
 SimpleMath()
.NET Remoting 编程一插图4.NET Remoting 编程一插图5        
.NET Remoting 编程一插图3
{
.NET Remoting 编程一插图6            
//

.NET Remoting 编程一插图6            
//
 TODO: 在此处添加构造函数逻辑
.NET Remoting 编程一插图6            
//
.NET Remoting 编程一插图7
        }

.NET Remoting 编程一插图6        
public int Add(int a ,int
 b)
.NET Remoting 编程一插图4.NET Remoting 编程一插图5        
.NET Remoting 编程一插图3
{
.NET Remoting 编程一插图6            Console.WriteLine(
"SimpleMath.Add({0},{1})"
,a,b);
.NET Remoting 编程一插图6            
return a+
b;
.NET Remoting 编程一插图7        }

.NET Remoting 编程一插图6        
public int Subtract(int a, int
 b)
.NET Remoting 编程一插图4.NET Remoting 编程一插图5        
.NET Remoting 编程一插图3
{
.NET Remoting 编程一插图6            Console.WriteLine(
"SimpleMath.Subtract({0},{1})"
,a,b);
.NET Remoting 编程一插图6            
return a-
b;
.NET Remoting 编程一插图7        }

.NET Remoting 编程一插图7    }

.NET Remoting 编程一插图8}

接着构建服务器端:

 1.NET Remoting 编程一插图using
 System;
 2.NET Remoting 编程一插图using

 System.Runtime.Remoting;
 3.NET Remoting 编程一插图using

 System.Runtime.Remoting.Channels;
 4.NET Remoting 编程一插图using

 System.Runtime.Remoting.Channels.Http;
 5.NET Remoting 编程一插图using

 MathLibrary;
 6

.NET Remoting 编程一插图
 7.NET Remoting 编程一插图namespace

 MathServer
 8.NET Remoting 编程一插图1.NET Remoting 编程一插图2.NET Remoting 编程一插图3

{
 9.NET Remoting 编程一插图4.NET Remoting 编程一插图5    /**/
/// <summary>
10.NET Remoting 编程一插图6    ///
 ServerMain 的摘要说明。
11.NET Remoting 编程一插图7    /// </summary>

12.NET Remoting 编程一插图6    public class
 ServerMain
13.NET Remoting 编程一插图4.NET Remoting 编程一插图5    .NET Remoting 编程一插图3
{
14.NET Remoting 编程一插图6        public
 ServerMain()
15.NET Remoting 编程一插图4.NET Remoting 编程一插图5        .NET Remoting 编程一插图3
{
16.NET Remoting 编程一插图6            //

17.NET Remoting 编程一插图6            //
 TODO: 在此处添加构造函数逻辑
18.NET Remoting 编程一插图6            
//
19.NET Remoting 编程一插图7        }

20.NET Remoting 编程一插图6        public static void Main(string
[] args)
21.NET Remoting 编程一插图4.NET Remoting 编程一插图5        .NET Remoting 编程一插图3
{
22.NET Remoting 编程一插图6            //建立一个通道并指定段口号

23.NET Remoting 编程一插图6            HttpChannel channel = new HttpChannel(13101
);
24.NET Remoting 编程一插图6            //通过运行时的远程服务来注册通道

25.NET Remoting 编程一插图6
            ChannelServices.RegisterChannel(channel);
26.NET Remoting 编程一插图6            //注册被调用类为已知对象well-know

27.NET Remoting 编程一插图6
            RemotingConfiguration.RegisterWellKnownServiceType(
28.NET Remoting 编程一插图6                typeof(MathLibrary.SimpleMath),//被注册的类

29.NET Remoting 编程一插图6                "MyURI.soap",                //已知对象名称Well-Know Name

30.NET Remoting 编程一插图6                WellKnownObjectMode.Singleton);    //被调用模式Singleton 或者 SingleCall

31.NET Remoting 编程一插图6
            
32.NET Remoting 编程一插图6            Console.WriteLine("Server started.Press Enter to End!"
);
33
.NET Remoting 编程一插图6            Console.ReadLine();            
34.NET Remoting 编程一插图7        }

35.NET Remoting 编程一插图7    }

36.NET Remoting 编程一插图8}


然后构建客户端,注意代码注释:
需要知道如下信息:服务器机器名,通道类型,端口,分配给远程对象的URI
(1)添加引用如下所示
(2)添加对程序集的引用,本例中为MathLibrary.dll
(3)使用与服务器相同的通道类型注册一个通道,本例中为HTTP
(4)调用Activator.GetObject()方法,传递合适的URL以取得一个远程对象的代理
(5)找代理强制转换成合适的类型,并把他当真实对象使用

 1 
using
 System;

 2 

using
 System.Runtime.Remoting;

 3 

using
 System.Runtime.Remoting.Channels;

 4 

using
 System.Runtime.Remoting.Channels.Http;

 5 

using
 MathLibrary;

 6 

namespace
 MathClient

 7 
{

 8 
    
///
 
<summary>


 9 
    
///
 ClientMain 的摘要说明。

10 
    
///
 
</summary>


11 

    
public
 
class
 ClientMain

12 
    {

13 
        
public
 ClientMain()

14 
        {

15 
            
//


16 
            
//
 TODO: 在此处添加构造函数逻辑

17 
            
//

18 

        }

19 
        
public
 
static
 
void
 Main(
string
[] args)

20 
        {

21 
            
//
创建并注册通道,没有指定段口号,所以不能接受消息


22 

            HttpChannel channel 
=
 
new
 HttpChannel();

23 
            ChannelServices.RegisterChannel(channel);

24 


25 
            
//
获取远程对象的一个代理


26 

            Object remoteObj 
=
 Activator.GetObject(
typeof
(MathLibrary.SimpleMath),
"
Http://localhost:13101/MyURI.soap
"
);

27 
            
//
将代理转换为所需要的类


28 

            SimpleMath math1 
=
 (SimpleMath)remoteObj;

29 


30 
            
//
方法2


31 

            SimpleMath math2 
=
 (SimpleMath)RemotingServices.Connect(

32 
                
typeof
(MathLibrary.SimpleMath),

33 
                
"
Http://localhost:13101/MyURI.soap
"


34 
                );

35 
            
//
方法3


36 

            RemotingConfiguration.RegisterWellKnownClientType(

37 
                
typeof
(MathLibrary.SimpleMath),

38 
                
"
Http://localhost:13101/MyURI.soap
"


39 
                );

40 
            SimpleMath math3 
=
 
new
 SimpleMath();

41 
            

42 
            

43 


44 
            Console.WriteLine(
"
5+2={0}
"
,math3.Add(
5
,
2
));

45 
            Console.WriteLine(
"
5-2={0}
"
,math3.Subtract(
5
,
2
));

46 
            Console.WriteLine(
"
Press enter to End
"
);

47 
            Console.ReadLine();

48 
        }

49 
    }

50 
}

转载于:https://www.cnblogs.com/fastcatcher/archive/2005/08/14/214350.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/95325148

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《.NET Remoting 编程一
   

还没有人抢沙发呢~