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

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

 

此处下载源代码(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.csusing System;using System.Collections.Generic;using System.Text;namespace RemotingModel{    ///     ///    ///    public class Talker:MarshalByRefObject    {       ///        /// 说话       ///        ///        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在局域网里的使用

 

转载地址:http://gchql.baihongyu.com/

你可能感兴趣的文章
javabean使用技巧
查看>>
JS/JQ综合总结
查看>>
CGAffineTransform相关函数
查看>>
字符编码与字符集区别与联系(网页/PHP文件/MYSQL数据库乱码问题)
查看>>
黑马程序员-----const和readonly的区别
查看>>
转载:基于MapXtreme的鹰眼功能
查看>>
黄聪:远程序桌面登录的.NET(C#)开发
查看>>
JMeter聚合报告(Aggregate Report)理解
查看>>
C# 多线程Thread.IsBackground=True的作用
查看>>
Oracle数据库安装问题记录
查看>>
Error:flask_sqlalchemy
查看>>
算法3-排序-简单选择排序
查看>>
poj 1743 Musical Theme (后缀数组)
查看>>
XACML学习
查看>>
Java中文乱码问题研究(二)
查看>>
easyui图标大全
查看>>
Emmet:HTML/CSS代码快速编写神器
查看>>
webpack实战
查看>>
虚幻4游戏开发_3_创建与继承材质
查看>>
win2003域控主备(热备)搭建
查看>>