C# Remoting 简单实现
2015-11-24 23:52
302 查看
此处下载源代码(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
注:以上所有操作均在同一台电脑,并且在同一个解决方案执行。
接下来会跟大家分享Remoting在局域网里的使用
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", 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在局域网里的使用
相关文章推荐
- C#推送安卓—极光推送
- c#中的DBNull和Null
- 第一个规范的C#类库
- C#(WPF)按钮图片不显示,壁纸切换功能
- VS2015 C#6.0
- C# FTP上传下载(支持断点续传)
- C# FTP上传下载(支持断点续传)
- VS2015 C#6.0 中的那些新特性
- 20151124001 关闭C#主窗体弹出是否关闭对话框
- C#命名规则
- C#~构造方法
- c# ConfigurationSection
- C#中的委托与事件
- c# 基础部分 (基本数据类型-- 表达式)
- C#跳转语句
- 轻松学习C#的正则表达式
- [转载收藏]C#基础知识梳理系列十一:垃圾回收机制
- C# XML解析
- C#中的内存四区
- 关于C#中对象用作属性