做开发很久了 Remoting 一直没有碰过,正好最近的项目上面用,就拿出来给大家看看
来源:互联网 发布:分享淘宝宝贝 编辑:程序博客网 时间:2024/04/29 21:12
首先 Remoting 严格来说分为3个部分, 服务端 ,客户端,中间件,中间件是指把要公布的方法和接口写成dll的形式,给客户端和服务端调用。
不说其他的了 ,直接上代码
RemotingModel 中间件 是建立的类库程序
Talker.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RemotingModel
{
public class Talker : MarshalByRefObject
{
/// <summary>
/// 说话
/// </summary>
/// <param name="word"></param>
public void Talk(string word)
{
System.Console.WriteLine(word);
}
}
}
此类中就写了一个方法, 然后要公布出去 用的 MarshalByRefObject
RemotingServer 服务端 是用的控制台程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingModel;
namespace RemotingServer
{
class Program
{
static void Main(string[] args)
{
//RemotingConfiguration.Configure(Application.ExecutablePath);
//注册通道
TcpServerChannel channel = new TcpServerChannel("TalkChannel", 8090); //端口随便取
ChannelServices.RegisterChannel(channel, true);
//注册远程对象
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Talker),
"Talker",
WellKnownObjectMode.SingleCall);
Console.WriteLine("请不要关此程序,3Q");
Console.ReadLine();
}
}
}
RemotingClient 客户端 是winform程序
我的客户端用的是一个窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingModel;
namespace RemotingClient
{
public partial class Form1 : Form
{
private Talker _talk = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//注册通道
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, true);
//获取远程对象
_talk = (Talker)Activator.GetObject(typeof(Talker), "TCP://10.4.21.121:8090/Talker");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_Send_Click(object sender, EventArgs e)
{
try
{
//操作远程对象
_talk.Talk(txtWord.Text.Trim());
txtContent.Text = "发送成功" + txtWord.Text.Trim();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
上面就是一个简单的remoting 结构的程序做完了,其实很简单的
代码下载 http://download.csdn.net/detail/zuoming120/3696247
- 做开发很久了 Remoting 一直没有碰过,正好最近的项目上面用,就拿出来给大家看看
- 最近写项目的时候用到了RSA加密,就来和大家分享一下 不过没有加签
- 免费的C语言和数据结构教学视频(找了很久才找到的好教程,拿出来分享给大家)
- 很久没来了,没有给朋友们回复.在这给大家道歉了。
- 有一段时间没有来过了,大家过的好吗?
- 好久没有更新了,该好好总结一下最近做过的开发了~~~
- BT的日本项目,我很久没有时间来了
- 最近真的很久没有写作了
- 最近没有做软件开发的项目
- 很久没有来了
- 很久没有来了
- 很久没有来了
- 很久没有来了
- 很久没有来了
- 昨天观看了凤舞烟老师的BF-TECH3.0之零基础3G Android移动开发,拿出来给大家分享下
- 就最近一段时间,就遇到了两次session id 一直变化的,所以做个总结。
- greybox的使用方法(感觉不错,就发上来给大家看看)
- 很久没来过了
- 获得OnOK退出控制
- 关于sourceinsight用ftp
- 3.11 parallel inheritance hierarchies (平行继承体系)
- 《Java 本地接口规范》- 设计概述
- 屏幕抓词的技术实现
- 做开发很久了 Remoting 一直没有碰过,正好最近的项目上面用,就拿出来给大家看看
- 去尼玛风险
- session的详细解释
- Android 显示安装程序列表及SimpleAdapter说明
- 再严重的感冒,马上就好!!
- 64位Windows操作系统“ ORA-12154: TNS: 无法解析指定的连接标识” 的解决方法
- 创始人和VC的推荐:企业家应该读读这21本书
- 对比C++和Java
- 3.12 lazy class(冗赘类)