此处下载源代码(VS2010编译通过)

  http://files.cnblogs.com/files/qqhfeng/%E8%BF%9C%E7%A8%8B%E8%B0%83%E7%94%A8%E6%B5%8B%E8%AF%952.rar

RemotingModel: Talker.cs

using System;
using System.Collections.Generic;
using System.Text; namespace RemotingModel
{
/// <summary>
///
/// </summary>
public class Talker:MarshalByRefObject
{
/// <summary>
/// 说话
/// </summary>
/// <param name="word"></param>
public void Talk(string word)
{
System.Console.WriteLine(word);
}
}
} 服务器端:是一个控制台,首先要添加对System.Runtime.Remoting的引用,然后添加对RemotingModel的引用 using System;
using System.Collections.Generic;
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)
{
//注册通道
TcpServerChannel channel = new TcpServerChannel("TalkChannel", ); //端口随便取
ChannelServices.RegisterChannel(channel, true); //注册远程对象
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Talker),
"Talker", WellKnownObjectMode.SingleCall); Console.ReadLine();
}
}
} 客服端:窗体:两个textBox,一个button,设置textBox为多行。上面的textBox为:txtContent,下面的为:txtWord
 

添加引用(添加方法同上)

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 btnSend_Click(object sender, EventArgs e)
{
try
{
//操作远程对象
_talk.Talk(txtWord.Text.Trim());
txtContent.Text = "发送成功" + txtWord.Text.Trim();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void Form1_Load(object sender, EventArgs e)
{
try {
//注册通道
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, true); //获取远程对象
_talk=(Talker) Activator.GetObject(typeof(Talker),"TCP://localhost:8090/Talker");
}
catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
}
}
好了,下面看看结果:
 

注:以上所有操作均在同一台电脑,并且在同一个解决方案执行。

接下来会跟大家分享Remoting在局域网里的使用

最新文章

  1. 【转】PHP网站(nginx、php-fpm、mysql) 用户权限解析
  2. MyBatis原理分析之三:初始化(配置文件读取和解析)
  3. docker 初探
  4. [HDOJ3466]Proud Merchants(贪心+01背包)
  5. 【剑指offer 面试题38】数字在排序数组中出现的次数
  6. silverlight视频、音频
  7. sql server 2008中id如何设为自增
  8. 关于Android配色 自适应颜色的实现
  9. Python 词云分析周杰伦《晴天》
  10. Devexpress dll搜集
  11. https 网络传输安全架设
  12. 水仙花在python3在pycharm的实现
  13. MySQL中EXPLAIN解释命令 查看索引是否生效
  14. css3学习系列之初识 transform (一)
  15. 用python开发android应用 【转载】
  16. 使用gitlab, jenkins搭建CI(持续集成)系统(2) -- 配置webhook触发构建
  17. 冲刺One之站立会议2
  18. crontab 格式
  19. 使用Frame控件设计Silverlight的导航
  20. Bootstrap的使用。。。

热门文章

  1. 写hibernate.cfg.xml时报错The content of element type &quot;property&quot; must match &quot;(meta*,(column|formula)*,type?)&quot;.
  2. mysql数据库优化课程---1、数据库的本质是什么
  3. Linux脚本基础
  4. Spring Boot配置文件详解:自定义属性、随机数、多环境配置
  5. 将一个jar包放到linux下定时执行
  6. DSD, DFF, DSF, DST概念解析
  7. SQLyog中的计算适合的数据类型
  8. lr中检查点的使用web_find()和web_reg_find()的区别
  9. nginx Win下实现简单的负载均衡(2)站点共享Session
  10. JavaScript中的数组和对象 增删遍