今天对C# Remoting进行了初步的学习,废话不说...

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", 8090); //端口随便取
            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. 第三方框架之ThinkAndroid 学习总结(二)
  2. CoreGraphics-基本图形绘制-直线、三角形、矩形、椭圆形、弧形
  3. C#可空类型的速度和GC Alloc测试
  4. Productivity Power Tools 的使用
  5. AngularJS快速入门指南06:过滤器
  6. kettle转换JavaScript加载外部js文件
  7. Java学习的随笔(一)对象概念、this指针、权限修饰符
  8. 002-C语言概览
  9. JDK安装(CentOS/rpm方式)
  10. tabBar自定义
  11. (简单) POJ 3159 Candies,Dijkstra+差分约束。
  12. js最常用正则表达式集合
  13. mysql日志分析工具之mysqlsla
  14. db2实现递归调用 机构等树形数据结构形成
  15. mysql 案例~关于pt-osc工具的用途
  16. 【转载】ASP.NET MVC的过滤器【Filters】
  17. ucos-iii串口用信号量及环形队列中断发送,用内建消息队列中断接收
  18. SQL语句(十四)子查询
  19. Shell初学(二)变量及数组
  20. 20145234黄斐《Java程序设计》实验五—网络安全与编程

热门文章

  1. GIT 的常见用法
  2. struct模块的使用
  3. Debug和Release区别(转)
  4. 字符串操作——C语言实现
  5. WGCNA构建基因共表达网络详细教程
  6. JavaScript设计模式小抄集(持续更新)
  7. OC学习篇之---总结和学习目录
  8. SCP-bzoj-1058
  9. 理解同步/异步/阻塞/非阻塞IO区别
  10. [CSP-S模拟测试]:简单的玄学(数学)