分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

.Net中Remoting通信机制简单实例

发布时间:2023-09-06 01:54责任编辑:熊小新关键词:暂无标签

原文:.Net中Remoting通信机制简单实例

.Net中Remoting通信机制

前言:

本程序例子实现一个简单的Remoting通信案例

  本程序采用语言:c#

  编译工具:vs2013工程文件

  编译环境:.net 4.0

程序模块:

  • Test测试
  • Talker
  • Server端
  • Client端
  • 源代码工程文件下载

Test测试程序截图:

Talker类:

1 public class Talker : MarshalByRefObject2 ????{3 ????????public void Talk(string word)4 ????????{5 ????????????System.Console.WriteLine(word);6 ????????}7 8 ????}

Server端:

1 ?//注册通道2 ????????????TcpServerChannel channel = new TcpServerChannel("TalkChannel",8090);3 ????????????ChannelServices.RegisterChannel(channel,true);4 5 ????????????//注册远程对象6 ????????????RemotingConfiguration.RegisterWellKnownServiceType(7 ????????????????typeof(Talker),8 ????????????????"Talker",9 ????????????????WellKnownObjectMode.SingleCall);

Client端:

 1 ??public partial class Form1 : Form 2 ????{ 3 ????????private Talker _talk = null; 4 ????????public Form1() 5 ????????{ 6 ????????????InitializeComponent(); 7 ????????} 8 ?9 ????????private void btnSend_Click(object sender, EventArgs e)10 ????????{11 ????????????if (btnSend.Text.Equals("开始"))12 ????????????{13 ????????????????timer1.Enabled = true;14 ????????????????btnSend.Text = "结束";15 ????????????}16 ????????????else17 ????????????{18 ????????????????timer1.Enabled = false;19 ????????????????btnSend.Text = "开始";20 ????????????}21 ????????}22 23 ????????private void sendMsg(string msg)24 ????????{25 ????????????try26 ????????????{27 ????????????????//操作远程对象28 ????????????????_talk.Talk(msg);29 ????????????????string newline = msg + Environment.NewLine;30 ????????????????txtContent.Text = txtContent.Text.Insert(0, newline);31 ????????????}32 ????????????catch (Exception ex)33 ????????????{34 ????????????????MessageBox.Show(ex.Message);35 ????????????}36 ????????}37 38 ????????private void Form1_Load(object sender, EventArgs e)39 ????????{40 ????????????try41 ????????????{42 ????????????????timer1.Interval = 1000;43 ????????????????//注册通道44 ????????????????TcpClientChannel channel = new TcpClientChannel();45 ????????????????ChannelServices.RegisterChannel(channel, true);46 ????????????????//获取远程对象47 ????????????????_talk = (Talker)Activator.GetObject(typeof(Talker), "TCP://localhost:8090/Talker");48 ????????????}49 ????????????catch (Exception ex)50 ????????????{51 ????????????????MessageBox.Show(ex.Message);52 ????????????}53 ????????}54 55 ????????private void timer1_Tick(object sender, EventArgs e)56 ????????{57 ????????????sendMsg(txtWord.Text.Trim());58 ????????}

源代码工程文件下载:

  源代码工程文件下载 http://files.cnblogs.com/files/JiYF/RemotingSolution.rar

.Net中Remoting通信机制简单实例

原文地址:https://www.cnblogs.com/lonelyxmas/p/9050782.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved