博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RemoTing 搭建简单实现
阅读量:7078 次
发布时间:2019-06-28

本文共 1992 字,大约阅读时间需要 6 分钟。

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

转载于:https://www.cnblogs.com/cenwenlin/p/9405181.html

你可能感兴趣的文章
【算法导论第13章】红黑树
查看>>
对PostgreSQL中bufmgr.c 中 bufs_to_lap的初步理解
查看>>
Windows 内存分析之路 --How to use Resource Monitor
查看>>
文件上传
查看>>
理解maven的核心概念
查看>>
一个简单的名片管理程序(C#)
查看>>
max tablename length limit in MySQL is 64
查看>>
Ubuntu 12.04 中国科学技术大学源
查看>>
(转)c#实现WinRAR解压缩
查看>>
MIME
查看>>
NetworkInterface的使用
查看>>
在IIS上启用Gzip压缩(HTTP压缩)
查看>>
解决ImportError: cannot import name webdriver
查看>>
如何将Windows Server 2012的Evaluation版本转为正式版?
查看>>
[iOS] UITextField隐藏软键盘心得(隐藏自身软键盘、点击Return自动转到下个文本框、轻触背景隐藏软键盘)...
查看>>
hdu 1853(Cyclic Tour)
查看>>
静态编译mysql库到程序中遇到的问题
查看>>
JavaScript Event Loop 浅析
查看>>
在独立的javascript文件中获取当前项目的http路径
查看>>
Yaf(Yet Another Framework)用户手册
查看>>