Remoting技术初探--实现简单服务器客户机通信
2008-04-03 11:14
288 查看
最近因工作需要研究了一下Remoting,因为以前一直在做Delphi开发,没有接触到这块,今天做了个小Demo,算是对Remoting有了一点浅显的认识,现把代码发布出来,希望大家指点。
A建立远程对象:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Lifetime;
namespace RemotingClass
...{
public class PublicCls : MarshalByRefObject//一定要继承这个对象
...{
public string Start;
//构造
public PublicCls()
...{
this.Start = "Welcome to Remoting Exemple!";
}
//一个计算算法,用于测试
public int calculate(int nI)
...{
if (nI < 1)
...{
return 0;
}
if (nI > 1 && nI <= 2)
...{
return 1;
}
else
...{
return calculate(nI - 1) + calculate(nI - 2);
}
}
}
}
B建立服务器程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
namespace RemotingServer
...{
public partial class FrmServer : Form
...{
public FrmServer()
...{
InitializeComponent();
}
private void btn_StartServer_Click(object sender, EventArgs e)
...{
try
...{
RemotingConfiguration.Configure(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);//读取配置文件内信息
this.lab_State.Text = "数据服务已开启!";
}
catch (Exception ex)
...{
string str = ex.Message;
this.lab_State.Text = str;
}
}
}
}
服务器端配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<server>
<wellknown
mode ="Singleton"
type ="RemotingClass.PublicCls,RemotingClass"
objectUri ="publiccls"/>
</server>
<channels>
<channel port="8086" ref="http"/>
</channels>
<!--leaseManagerPollTime="7S"-->
<lifetime
leaseTime ="7M"
sponsorshipTimeout="7M"
renewOnCallTime="7M"/>
</application>
</system.runtime.remoting>
</configuration>
C建立客户机程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using RemotingClass;
namespace RemotingClient
...{
public partial class FrmClient : Form
...{
public FrmClient()
...{
InitializeComponent();
}
private PublicCls pcls = new PublicCls();//实例化一个远程对象
private void FrmClient_Load(object sender, EventArgs e)
...{
RemotingConfiguration.Configure(@"client.exe.config",false);//读取配置文件
if (pcls == null) //如果对象不存在
...{
this.lab_isConncetion.Text = "Cann't conncetion the Server!";
}
else ...{
this.lab_isConncetion.Text = "Conncetioned the Server!";
}
}
private void btn_Cal_Click(object sender, EventArgs e)
...{
try
...{
//使用远程对象进行计算
int i = pcls.calculate(Convert.ToInt32(this.txt_Num.Text.Trim()));
this.lab_Cal.Text = i.ToString();
}
catch(FormatException ex)
...{
string str = ex.Message;
this.lab_Cal.Text = "输入的不是数字!";
}
}
}
}
客户机配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown type ="RemotingClass.PublicCls,RemotingClass"
url ="http://192.168.0.117:8086/publiccls"/>
</client>
<channels>
<channel ref="http" port="0"></channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
A建立远程对象:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Lifetime;
namespace RemotingClass
...{
public class PublicCls : MarshalByRefObject//一定要继承这个对象
...{
public string Start;
//构造
public PublicCls()
...{
this.Start = "Welcome to Remoting Exemple!";
}
//一个计算算法,用于测试
public int calculate(int nI)
...{
if (nI < 1)
...{
return 0;
}
if (nI > 1 && nI <= 2)
...{
return 1;
}
else
...{
return calculate(nI - 1) + calculate(nI - 2);
}
}
}
}
B建立服务器程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
namespace RemotingServer
...{
public partial class FrmServer : Form
...{
public FrmServer()
...{
InitializeComponent();
}
private void btn_StartServer_Click(object sender, EventArgs e)
...{
try
...{
RemotingConfiguration.Configure(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);//读取配置文件内信息
this.lab_State.Text = "数据服务已开启!";
}
catch (Exception ex)
...{
string str = ex.Message;
this.lab_State.Text = str;
}
}
}
}
服务器端配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<server>
<wellknown
mode ="Singleton"
type ="RemotingClass.PublicCls,RemotingClass"
objectUri ="publiccls"/>
</server>
<channels>
<channel port="8086" ref="http"/>
</channels>
<!--leaseManagerPollTime="7S"-->
<lifetime
leaseTime ="7M"
sponsorshipTimeout="7M"
renewOnCallTime="7M"/>
</application>
</system.runtime.remoting>
</configuration>
C建立客户机程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using RemotingClass;
namespace RemotingClient
...{
public partial class FrmClient : Form
...{
public FrmClient()
...{
InitializeComponent();
}
private PublicCls pcls = new PublicCls();//实例化一个远程对象
private void FrmClient_Load(object sender, EventArgs e)
...{
RemotingConfiguration.Configure(@"client.exe.config",false);//读取配置文件
if (pcls == null) //如果对象不存在
...{
this.lab_isConncetion.Text = "Cann't conncetion the Server!";
}
else ...{
this.lab_isConncetion.Text = "Conncetioned the Server!";
}
}
private void btn_Cal_Click(object sender, EventArgs e)
...{
try
...{
//使用远程对象进行计算
int i = pcls.calculate(Convert.ToInt32(this.txt_Num.Text.Trim()));
this.lab_Cal.Text = i.ToString();
}
catch(FormatException ex)
...{
string str = ex.Message;
this.lab_Cal.Text = "输入的不是数字!";
}
}
}
}
客户机配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown type ="RemotingClass.PublicCls,RemotingClass"
url ="http://192.168.0.117:8086/publiccls"/>
</client>
<channels>
<channel ref="http" port="0"></channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
相关文章推荐
- UDP简单实现并发服务器和客户端简单通信
- 网络编程:使用Socket实现简单的服务器和客户端的通信
- JAVA 实现 EchoServer EchoClient 服务器、客户机本地通信程序
- Python实现udp协议的简单服务器和客户端通信
- Android简单实现Socket通信,客户端连接服务器后,服务器向客户端发送文字数据
- ACE+线程池实现一个简单的服务器+多客户端通信程序
- 从简单到复杂实现TCP客户端服务器通信
- C#使用Socket实现服务器与多个客户端通信(简单的聊天系统)
- JAVA 实现 EchoServer EchoClient 服务器、客户机本地通信程序
- Linux C TCP Socket实现客户与服务器简单通信
- Linux下socket编程实现客户机服务器通信的例子
- [20180313智慧餐厅推荐系统02]基于python的socket编程代码,实现PC与服务器的简单通信
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
- 两部android设备通过服务器转发实现通信简单demo
- Java实现服务器和客户端简单通信
- 服务器—客户端模式实现简单的通信
- Linux C实现简单的UDP服务器客户端通信
- 用Java socket (TCP通信模型)实现一个简单的web 服务器
- Linux C TCP Socket实现客户与服务器简单通信
- [Java] 【源码】新手初探java网络编程——socket接口实现简单的客户机/服务器程序