[转]Remoting的一个代码示例(借助Remoting实现发送信息功能)

本文介绍了一个基于.NET框架的远程调用示例,包括客户端和服务端的实现方式,通过TCP信道实现信息发送功能。
None.gif(一).说明
None.gif    一个远程调用示例. 
None.gif    此示例实现功能:  客房端调用远程方法(远程方法可以弹    出自定义信息),实现发送信息功能. 
None.gif    实现原理概是这样的:客户端不能直接调用远程对象,它必须先通过信道请求服务端宿主程序,当收到客户端请求时,
None.gif    .net远程处理框架会在宿主组件的应用程序域中生成所需要的远程对象. 并执行远程对象中的方法.     
None.gif(二).实现方案
None.gif  在之前先介绍几种类:
None.gif    
1.可序列化的类:  以<serializable>属性为标记,可以在进程/应用程序/计算机之间传送.
None.gif    
2.可远程调用的类: 直接或间接地继承 System.MarshalByRefObject类,可以被远程激活.
None.gif    
3.一般类:         不能构建分布式,用于本地调用.
None.gif  
1.首先建立三个项目: 
None.gif    RemoteObject: 提供远程对象,供客户端调用  
None.gif    SimpleClient: 用于向服务端程序发出请求,调用远程对象 (winform)
None.gif    SimpleServer: 侦听客户端请求,并创建对象             (winform) 
None.gif  
2.在RemoteObject项目下面建立远程调用类: RemoteObject.cs
None.gif    在SimpleClient项目下面建立: Form1.cs和SimpleClient.exe.config配置文件。 
None.gif          其中配置文件的作用是指定服务端地址和信道等信息,下面的代码里面有详细说明.
None.gif    在SimpleServer项目下面建立: Form1.cs和SimpleServer.exe.config配置文件。
None.gif          其中配置文件的作用是指定接受请求客户端的地址和信道等信息,下面的代码里面有详细说明.  
None.gif(三).
None.gif  各文件源代码:
None.gif  
1.RemoteObject.cs
None.gif    
using System;
None.gif    
using System.Windows.Forms;
None.gif    
namespace RemoteObjects
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif 
public class RemoteObject : System.MarshalByRefObject  //继承此类才能被远程激活调用
ExpandedSubBlockStart.gifContractedSubBlock.gif
 dot.gif{
InBlock.gif  
public RemoteObject()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
//远程调用方法,功能: 弹出自定义消息, 参数: str是从客户端传递过来的
InBlock.gif
  public static void Method(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif                   MessageBox.Show(str);   
ExpandedSubBlockEnd.gif  }
  
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif    }

None.gif  
2.客户端工程SimpleClient项目中的Form1.cs:
None.gif    
using System;
None.gif    
using System.Drawing;
None.gif    
using System.Collections;
None.gif    
using System.ComponentModel;
None.gif    
using System.Windows.Forms;
None.gif    
using System.Data;
None.gif    
using RemoteObjects;
None.gif    
namespace SimpleClient
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif 
public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
private System.Windows.Forms.TextBox textBox1;
InBlock.gif  
private System.Windows.Forms.Label label1;
InBlock.gif  
private System.Windows.Forms.Button button1;  
InBlock.gif  
private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif  
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif   InitializeComponent();
ExpandedSubBlockEnd.gif  }
  
InBlock.gif  
protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     components.Dispose();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
base.Dispose( disposing );
ExpandedSubBlockEnd.gif  }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif  
/// 此方法的内容。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
this.textBox1 = new System.Windows.Forms.TextBox();
InBlock.gif   
this.label1 = new System.Windows.Forms.Label();
InBlock.gif   
this.button1 = new System.Windows.Forms.Button();
InBlock.gif   
this.SuspendLayout();
InBlock.gif   
// 
InBlock.gif   
// textBox1
InBlock.gif   
// 
InBlock.gif
   this.textBox1.Location = new System.Drawing.Point(4088);
InBlock.gif   
this.textBox1.Multiline = true;
InBlock.gif   
this.textBox1.Name = "textBox1";
InBlock.gif   
this.textBox1.Size = new System.Drawing.Size(336176);
InBlock.gif   
this.textBox1.TabIndex = 0;
InBlock.gif   
this.textBox1.Text = "";
InBlock.gif   
// 
InBlock.gif   
// label1
InBlock.gif   
// 
InBlock.gif
   this.label1.Location = new System.Drawing.Point(4032);
InBlock.gif   
this.label1.Name = "label1";
InBlock.gif   
this.label1.TabIndex = 1;
InBlock.gif   
this.label1.Text = "请输入信息:";
InBlock.gif   
// 
InBlock.gif   
// button1
InBlock.gif   
// 
InBlock.gif
   this.button1.Location = new System.Drawing.Point(29632);
InBlock.gif   
this.button1.Name = "button1";
InBlock.gif   
this.button1.TabIndex = 2;
InBlock.gif   
this.button1.Text = "发送";
InBlock.gif   
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif   
// 
InBlock.gif   
// Form1
InBlock.gif   
// 
InBlock.gif
   this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif   
this.ClientSize = new System.Drawing.Size(416302);
InBlock.gif   
this.Controls.Add(this.button1);
InBlock.gif   
this.Controls.Add(this.label1);
InBlock.gif   
this.Controls.Add(this.textBox1);
InBlock.gif   
this.Name = "Form1";
InBlock.gif   
this.Text = "发送信息";
InBlock.gif   
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif   
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif  
InBlock.gif  [STAThread]
InBlock.gif  
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   Application.Run(
new Form1());   
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif      
//载入信道,用于侦听客户端请求,配置文件记载客户端地址等信息.
InBlock.gif
   System.Runtime.Remoting.RemotingConfiguration.Configure("Simpleclient.exe.config");   
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//开始调用远程对象(发送信息)
InBlock.gif
   RemoteObjects.RemoteObject.Method(this.textBox1.Text);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif    }

None.gif
3.客户端工程SimpleClient项目中的SimpleClient.exe.config:
None.gif   
<?xml version="1.0" encoding="utf-8" ?>
None.gif   
<configuration>
None.gif 
<system.runtime.remoting>
None.gif  
<application name="simpleclient">
None.gif   
<!-- 指定请求的服务端地址和端口号-->
None.gif   
<!-- url中的:localhost是测试的本机,可以使用Intenet上的其它机器名或ip地址-->
None.gif   
<client url="tcp://localhost:8080/simpleserver">
None.gif    
<activated type="RemoteObjects.RemoteObject,RemoteObjects">
None.gif    
</activated>
None.gif   
</client>
None.gif   
<channels>
None.gif     
<!-- 指定信道,有两种信道可选:Tcp(基于TCP协议)和Http(无连续连接协议)信道-->
None.gif    
<channel ref="tcp client"/>   
None.gif   
</channels>
None.gif  
</application>
None.gif 
</system.runtime.remoting>
None.gif    
</configuration>
None.gif
4.服务端工程SimpleServer项目中的Form1.cs:
None.gif   
using System;
None.gif   
using System.Drawing;
None.gif   
using System.Collections;
None.gif   
using System.ComponentModel;
None.gif   
using System.Windows.Forms;
None.gif   
using System.Data;
None.gif   
using System.Runtime.Remoting;
None.gif
None.gif   
namespace SimpleServer
ExpandedBlockStart.gifContractedBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif  
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//
InBlock.gif   
// Windows 窗体设计器支持所必需的
InBlock.gif   
//
InBlock.gif
   InitializeComponent();
InBlock.gif
InBlock.gif   
//
InBlock.gif   
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif   
//
ExpandedSubBlockEnd.gif
  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     components.Dispose();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
base.Dispose( disposing );
ExpandedSubBlockEnd.gif  }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif  
/// 此方法的内容。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
// 
InBlock.gif   
// Form1
InBlock.gif   
// 
InBlock.gif
   this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif   
this.ClientSize = new System.Drawing.Size(292266);
InBlock.gif   
this.Name = "Form1";
InBlock.gif   
this.Text = "Form1";
InBlock.gif   
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  [STAThread]
InBlock.gif  
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//Application.Run(new Form1());   
InBlock.gif
   Run();
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
private static void Run()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   System.Runtime.Remoting.RemotingConfiguration.Configure(
"SimpleServer.exe.config");
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif    }

None.gif
5.服务端工程SimpleServer项目中的SimpleServer.exe.config:
None.gif  
<?xml version="1.0" encoding="utf-8" ?>
None.gif  
<configuration>
None.gif 
<system.runtime.remoting>
None.gif  
<application name="simpleserver">
None.gif   
<service>
None.gif    
<activated type="RemoteObjects.RemoteObject,RemoteObjects">
None.gif    
</activated>
None.gif   
</service>
None.gif   
<channels>
None.gif    
<channel ref="tcp server" port="8080" />
None.gif   
</channels>
None.gif  
</application>
None.gif 
</system.runtime.remoting>
None.gif  
</configuration>
None.gif(四).注意点
None.gif   
1.由于配置文件默认是添加在根目录下的,要把两个配置文件拷贝到:根目录/bin/debug下面,要让它与执行文件在同一个目录下面。
None.gif   
2.右击RemoteObject项目,选“常规”下的,输入类型为:“类库”.  它默认为应用程序,这里作为类库使用.  
None.gif     设置好后,按: Ctrl
+Shift+B生成类库Dll.
None.gif   
3.在工程SimpleServer和SimpleClient中分别添加引用: 即将RemoteObject项目刚生成的DLL: RemoteObjects.dll添加到各自的工程中.
None.gif     具体方法:右击“引用“
->”添加引用“->"浏览",找到生成的RemoteObjects.dll分别添加进来.
None.gif   
4.右击解决方案,选择“属性”-> “选中多启动项目单选框”->"选SimpleServer和SimpleClient同时启动".
None.gif     因为: 当服务端宿主程序运行时,客户端才能正确调用远程对象.
None.gif   
5.按F5运行.  输入信息,点“发送”按钮,就可以调用远程对象了.
None.gif(五).扩展
None.gif     可以修改客户端配置文件:
<client url="tcp://localhost:8080/simpleserver">  中的localhost为其它的机器名称,只要另一台
None.gif     机器运行了宿主程序,并处于运行状态. 那么当调用时,会在服务端弹出信息,即实现了发送信息功能.
None.gif
None.gif
None.gif以上代码已经测试,不正确的地方望批评指正
!
None.gif
None.gif

转载于:https://www.cnblogs.com/nbwzy/archive/2007/05/23/757432.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值