Remoting技术初探--实现简单服务器客户机通信
来源:互联网 发布:sqlserver安装polybase 编辑:程序博客网 时间:2024/05/22 00:09
最近因工作需要研究了一下Remoting,因为以前一直在做Delphi开发,没有接触到这块,今天做了个小Demo,算是对Remoting有了一点浅显的认识,现把代码发布出来,希望大家指点。
A建立远程对象:
B建立服务器程序
服务器端配置文件
C建立客户机程序
客户机配置文件
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);
}
}
}
}
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);
}
}
}
}
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;
}
}
}
}
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>
<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>
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 = "输入的不是数字!";
}
}
}
}
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>
<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>
- Remoting技术初探--实现简单服务器客户机通信
- [Java] 【源码】新手初探java网络编程——socket接口实现简单的客户机/服务器程序
- JAVA 实现 EchoServer EchoClient 服务器、客户机本地通信程序
- Linux下socket编程实现客户机服务器通信的例子
- Linux下socket编程实现客户机服务器通信的例子
- NIO客户机服务器通信示例
- 客户机/服务器一对一通信模型
- vc学习之服务器客户机通信
- SocketChannel的NIO客户机服务器通信实例
- UDP协议的客户机和服务器通信
- C# Remoting 简单实现
- Windows终端与瘦客户机/服务器技术
- 利用Socket实现两个客户机通信
- 简单的流服务器和流客户机
- 一个简单的客户机服务器程序
- 简单的客户机服务器投射模拟
- 一个简单的服务器和客户机程序
- 实现服务器和客户机的交互
- javascript code all (转转)
- 用PHP控制您的浏览器cache
- 窗体.show竟然无效
- 在Linux下快速搭建LAMP开发平台
- 实战SSL安全加密机制
- Remoting技术初探--实现简单服务器客户机通信
- How to add MFC Common class into your application based on SDK?
- Oreilly好书征集译者!
- 故宫一游
- AJAX读取XML赋值给下拉列表
- SELinux和maxent
- 对widget(挂件技术)的一点看法
- javascript code all (2) (转转)
- Windows Shell 编程 第四章