Webcast.NET Remoting学习笔记(1)第一个remoting程序

最近开始学习分布式程序设计,在网上下了msdn的视频教程,边看边学边记笔记,坚持把教程全部学习一遍。

先以一个简单的例子开始.NET Remoting的学习。
开发Remoting有三步:
1、创建远程对象
2、创建一个应用程序作为宿主,接受客户端的请求。
3、创建一个客户端调用远程对象。
下面开始第一步:
在解决方案中添加类库项目General

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  RemotingSamples
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class HelloServer : MarshalByRefObject
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public HelloServer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"HelloServer activated");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public String HelloMethod(String name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"收到 : {0}", name);
InBlock.gif            
return "服务器收到:" + name;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
其中HelloServe继承自MarshalByRefObject,而MarshalByRefObject继承自System.Object(不可远程化),在remoting中对象的传递有两种方式,一种是通过引用,即上面这种,还有一种是通过值传递,通过引用是指客户端得到对象的引用,在本地创建代理,通过代理来访问对象。值传递是得到远程对象的副本,对副本的操作不影响远程的对象。
第二步,创建宿主应用程序
宿主程序主要做以下几件事:
1、注册通道,remoting有两种内置通道tcp和http
2、注册服务器激活的远程对象
3、运行宿主程序
在解决方案中加入控制台应用程序Server
None.gif using  System;
None.gif
using  System.Runtime.Remoting;
None.gif
using  System.Runtime.Remoting.Channels;
None.gif
using  System.Runtime.Remoting.Channels.Tcp;
None.gif
using  System.Runtime.Remoting.Channels.Http;
None.gif
None.gif
namespace  RemotingSamples 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
public class Server
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static int Main(string [] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            TcpChannel chan1 
= new TcpChannel(8085);
InBlock.gif            HttpChannel chan2 
= new HttpChannel(8086);
InBlock.gif
InBlock.gif            ChannelServices.RegisterChannel(chan1);
InBlock.gif            ChannelServices.RegisterChannel(chan2);
InBlock.gif
InBlock.gif
InBlock.gif            RemotingConfiguration.RegisterWellKnownServiceType
InBlock.gif                (
InBlock.gif                
typeof(HelloServer),
InBlock.gif                
"SayHello",
InBlock.gif                WellKnownObjectMode.Singleton
InBlock.gif                );
InBlock.gif            
InBlock.gif
InBlock.gif            System.Console.WriteLine(
"Press Enter key to exit");
InBlock.gif            System.Console.ReadLine();
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
在这里注册了两条通道,chan1和chan2,分别是tcp和http通道。
第三步,建立客户端程序
客户端主要作以下几种事情:
1、注册通道
2、根据url得到对象代理
3、通过代理调用远程对象
在解决方案中加入控制台应用程序Client
None.gif using  System;
None.gif
using  System.Runtime.Remoting;
None.gif
using  System.Runtime.Remoting.Channels;
None.gif
using  System.Runtime.Remoting.Channels.Tcp;
None.gif
using  System.Runtime.Remoting.Channels.Http;
None.gif
using  System.IO;
None.gif
None.gif
namespace  RemotingSamples 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Client
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//使用TCP通道得到远程对象
InBlock.gif
            TcpChannel chan1 = new TcpChannel();
InBlock.gif            ChannelServices.RegisterChannel(chan1);
InBlock.gif            HelloServer obj1 
= (HelloServer)Activator.GetObject(
InBlock.gif                
typeof(RemotingSamples.HelloServer),
InBlock.gif                
"tcp://localhost:8085/SayHello");
InBlock.gif            
if (obj1 == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Console.WriteLine(
InBlock.gif                    
"Could not locate TCP server");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//使用HTTP通道得到远程对象
InBlock.gif
            HttpChannel chan2 = new HttpChannel();
InBlock.gif            ChannelServices.RegisterChannel(chan2);
InBlock.gif            HelloServer obj2 
= (HelloServer)Activator.GetObject(
InBlock.gif                
typeof(RemotingSamples.HelloServer),
InBlock.gif                
"http://localhost:8086/SayHello");
InBlock.gif            
if (obj2 == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Console.WriteLine(
InBlock.gif                    
"Could not locate HTTP server");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Console.WriteLine(
InBlock.gif                
"Client1 TCP HelloMethod {0}",
InBlock.gif                obj1.HelloMethod(
"服务器收到请回答1"));
InBlock.gif            Console.WriteLine(
InBlock.gif                
"Client2 HTTP HelloMethod {0}",
InBlock.gif                obj2.HelloMethod(
"服务器收到请回答2"));
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
然后开始执行(不调试)server,之后对client执行“启动新实例”可以看到结果
1.bmp

转载于:https://www.cnblogs.com/stuhrbeu/archive/2007/02/02/638328.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值