今天对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在局域网里的使用

RemoTing 搭建简单实现的更多相关文章

  1. 树莓派(Raspberry Pi)搭建简单的lamp服务

    树莓派(Raspberry Pi)搭建简单的lamp服务: 1. LAMP 的安装 sudo apt-get install apache2 mysql-server mysql-client php ...

  2. 拿nodejs快速搭建简单Oauth认证和restful API server攻略

    拿nodejs快速搭建简单Oauth认证和restful API server攻略:http://blog.csdn.net/zhaoweitco/article/details/21708955 最 ...

  3. Ubuntu 14.04搭建简单git服务器

    /****************************************************************************** * Ubuntu 14.04搭建简单gi ...

  4. 转:windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)

    原文来自于:http://www.cnblogs.com/zhuyp1015/archive/2012/06/16/2552269.html 使用gvim在windows环境下搭建简单的IDE环境可以 ...

  5. [angularjs] MVC + Web API + AngularJs 搭建简单的 CURD 框架

    MVC + Web API + AngularJs 搭建简单的 CURD 框架 GitHub 地址:https://github.com/liqingwen2015/Wen.MvcSinglePage ...

  6. nodejs辅助前台开发系列(1) 搭建简单HTML开发环境

    搭建简单的html开发环境一般需要解决两个问题: 文本编辑器 WebServer集成 在文本编辑器选择上,VS Code 无疑是一匹黑马,谁用谁知道.WebServer集成nodejs对前端来说最为友 ...

  7. mongoDB介绍、安装、搭建简单的mongoDB服务器(一)

    相关网站 1. http://www.mongodb.org/ 官网,可以下载安装程序,和doc,和驱动等. 2. http://www.mongoing.com/ 国内官方网站,博客,问题谈论等  ...

  8. Nginx之使用nginx搭建简单的文件服务器

    使用nginx可以搭建简单文件服务器 安装nginx(不详述) 修改配置文件 /usr/local/nginx/conf/nginx.conf user root; /usr/local/nginx/ ...

  9. 基于python2【重要】怎么自行搭建简单的web服务器

    基本流程:1.需要的支持     1)python本身有SimpleHTTPServer     2)ForkStaticServer.py支持,该文件放在python7目录下     3)将希望共享 ...

随机推荐

  1. C#编程--第二天

    一.变量:变量先声明,后赋值,再使用. 语法:变量类型 变量名=值: 变量类型: 分为基本数据类型和引用类 基本数据类型:整型.浮点型.字符型.布尔型 引用类:字符串.日期时间.枚举类型.结构类型 i ...

  2. .net Core AJAX使用Header传递参数,以JsonResult返回信息

    function postHeader() { $.ajax({ url : "/myTest/PostHeader?time="+ (new date()).getTime(), ...

  3. 一、bootstrap-fontawesome-iconpicker组件

    一.bootstrap-fontawesome-iconpicker组件 <!DOCTYPE html> <html lang="en"> <head ...

  4. Django 与 Flask框架的比较

    Django Django恐怕是最有代表性的Python框架了.它是一个遵循MMVC架构模式的开源框架.它的名字来自Django Reinhardt,一个法国作曲家和吉他演奏家,很多人认为他是历史上最 ...

  5. 【leetcode】662. Maximum Width of Binary Tree

    题目如下: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...

  6. 【leetcode】937. Reorder Log Files

    题目如下: You have an array of logs.  Each log is a space delimited string of words. For each log, the f ...

  7. 【多线程】LinkedTransferQueue

    LinkedTransferQueue是JDK1.7才添加的阻塞队列,基于链表实现的FIFO无界阻塞队列,是ConcurrentLinkedQueue(循环CAS+volatile 实现的wait-f ...

  8. win7 SP1 原版 32位 百度网盘下载

    下载地址:https://pan.baidu.com/s/1o6I410XduG1kcmn9vQ3miw 提取码:15vm 扫码下载:

  9. [CSP-S模拟测试]:卡常题/b(基环树+DP)

    题目描述 $ρ$有一个二分连通无向图,$X$方点.$Y$方点均为$n$个(编号为$1\sim n$).这个二分图比较特殊,每一个$Y$方点的度为$2$,一条黑色边,一条白色边.所有黑色边权值均为$a$ ...

  10. ZROI week2

    \[ZROI week2\] 除草机 首先考虑最少的拐点肯定是那种螺旋形状的,然后手玩几个数据发现和列数(行数)有关,且每增加1就是上一个状态加2,直接\(O(1)\)公式即可 吐槽:为啥\(n,m\ ...