.Net中Remoting通信的应用,有发送和返回信息

简介: 两个界面如图: 代码: 服务端 using System; using System.Collections.Generic; using System.

两个界面如图:



代码:

服务端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;//添加引用
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;
namespace Server
{
    /// <summary>
    /// 服务端接收、处理并返回客户端面、显示信息到界面
    /// </summary>
    public partial class Form1 : Form
    {
        string FilePath="";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            FilePath = AppDomain.CurrentDomain.BaseDirectory + "\\msg.txt";
            if (FilePath != "" && File.Exists(FilePath))
            {
                dothings.MsgContentLast+=File.ReadAllText(FilePath);
            }
            //注册通道
            IChannel channel = null;
            //channel = new TcpServerChannel("TalkChannel", 8090); //端口随便取
             channel = new HttpServerChannel(8091);
            ChannelServices.RegisterChannel(channel, false);//对于Http通道必须设置为false,否则将抛出:无法保证信道 http server 的安全。请考虑使用实现 ISecurableChannel 的信道 的异常

            //注册远程对象
            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(dothings),
                "dothings",
                WellKnownObjectMode.SingleCall);
        }

        private void timetick_Tick(object sender, EventArgs e)
        {
            if (dothings.MsgContentLast.ToString() != dothings.MsgContent.ToString())//有新信息才更新和滚动到最后
            {
                txtReceive.Text = dothings.MsgContentLast.ToString();//txtReceive为RichTextBox控件
                dothings.MsgContent = dothings.MsgContentLast;
                this.txtReceive.Select(this.txtReceive.TextLength, 0);//光标移到最后
                this.txtReceive.ScrollToCaret();//滚动到光标处
            }
        }
    }
    public class dothings : MarshalByRefObject
    {
        private static string _MsgContentLast ="";
        public static string MsgContentLast//新信息
        {
            get { return _MsgContentLast; }
            set { _MsgContentLast = value; }
        }
       
        private static string _MsgContent="";
        public static string MsgContent//旧信息
        {
            get { return _MsgContent; }
            set { _MsgContent = value; }
        }
       
       
        private string FilePath = AppDomain.CurrentDomain.BaseDirectory + "\\msg.txt";
        public string dothing(string msg)
        {
            File.AppendAllText(FilePath, msg + "\r\n");
            MsgContentLast+=("\r\n" + msg);
            return "发送成功,返回:" + msg;
        }
    }
}

客户端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using Server;

namespace Client
{
    /// <summary>
    /// 客户端发送、接收、显示返回的信息
    /// </summary>
    public partial class Form1 : Form
    {
        private dothings _dothings = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //注册通道
                IChannel channel=null;
               // channel = new TcpClientChannel();
                channel = new HttpClientChannel();
                ChannelServices.RegisterChannel(channel, true);
                //获取远程对象
                //_dothings = (dothings)Activator.GetObject(typeof(dothings), "TCP://localhost:8090/dothings");
                _dothings = (dothings)Activator.GetObject(typeof(dothings), "http://localhost:8091/dothings");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        string ReceiveMsg = "";
        private void btn_send_Click(object sender, EventArgs e)
        {
            try
            {
                //操作远程对象
                ReceiveMsg=_dothings.dothing(txtTosend.Text.Trim());//返回来的消息
                txtSended.Text += "\r\n" + ReceiveMsg;//txtSended为TextBox控件
                txtTosend.Text = "";
                txtTosend.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtTosend_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyValue == 13)//按Ctrl+Enter键就点击发送按钮
            {
                e.Handled = true;
                this.btn_send_Click(this, null);
            }
        }
    }
}
 

相关文章
|
5小时前
|
算法 Java 调度
|
6月前
|
SQL 数据库 开发工具
0002net程序设计-net家电维修保养信息系统
0002net程序设计-net家电维修保养信息系统
27 0
|
5小时前
|
C# Windows
一款.NET开源、简洁易用的Windows桌面小说阅读应用
一款.NET开源、简洁易用的Windows桌面小说阅读应用
|
5小时前
|
开发框架 物联网 测试技术
【专栏】.NET 开发:打造领先应用的基石
【4月更文挑战第29天】本文探讨了.NET开发框架为何成为构建领先应用的首选。高性能与稳定性是.NET的核心优势,它采用先进的技术和优化策略,如.NET Core的轻量级设计和JIT/AOT编译模式。跨平台兼容性让开发者能用相同代码库在不同操作系统上构建应用。现代化的开发体验,如C#语言的创新特性和Visual Studio的强大工具,提升了开发者生产力。丰富的生态系统和广泛支持,包括庞大的开发者社区和微软的持续投入,为.NET提供了坚实后盾。
|
5小时前
|
机器学习/深度学习 人工智能 Cloud Native
【专栏】洞察.NET 技术的前沿应用
【4月更文挑战第29天】本文探讨了.NET技术的前沿应用,包括.NET Core的跨平台崛起、云原生及AI/机器学习领域的整合。.NET Core支持多平台运行,开源社区的参与促进了其快速发展和性能优化。Xamarin与.NET MAUI助力跨平台移动应用和统一界面开发,而云原生应用借助.NET Core与Azure云服务得以轻松构建和部署。此外,ML.NET和TensorFlow.NET为.NET开发者提供了机器学习和深度学习工具,推动智能应用和边缘计算的创新。.NET技术正持续演进,引领软件开发新趋势。
|
5小时前
|
人工智能 物联网 开发者
【专栏】探究.NET 技术的创新应用
【4月更文挑战第29天】本文探讨了.NET技术的最新进展和创新应用,包括.NET 5及后续版本的统一平台、性能提升、跨平台支持、云集成优化和开源社区的贡献。在创新应用场景中,重点介绍了微服务架构、物联网、AI、游戏开发和移动应用。未来,.NET将持续优化性能,深化云原生应用,集成新兴技术,扩大社区生态,并促进相关教育和培训。开发者应把握.NET技术的潜力,积极参与其发展,创造更多创新软件产品。
|
5小时前
|
安全 Linux API
【专栏】.NET 开发:打造卓越应用的秘诀
【4月更文挑战第29天】本文介绍了.NET技术的起源、核心特性和应用场景,揭示了其打造卓越应用的秘诀。自2002年推出,.NET历经发展,现支持跨平台,包括.NET Core和.NET 5。其核心特性包括:跨平台兼容性、面向对象编程、内置安全性和高效性能。丰富的类库、强大的开发工具、简洁的语言语法以及活跃的社区支持,使.NET成为构建高效、安全应用的理想选择。随着技术进步,.NET将持续赋能开发者创造更多可能性。
|
5小时前
|
人工智能 安全 API
【专栏】理解 .NET 技术,打造优质应用
【4月更文挑战第29天】本文探讨了如何利用.NET技术构建高质量应用程序,介绍了.NET从2002年发展至今的历程,强调其跨平台能力、高效开发、丰富的类库和API、开源生态及安全性等优势。随着.NET 6的规划,平台将更加统一和跨平台,适应云计算、AI等新兴技术。.NET凭借其特性,成为开发者和企业创新的有力工具,未来将继续扮演重要角色。
|
5小时前
|
机器学习/深度学习 自然语言处理 安全
【专栏】.NET 开发:构建智能应用的关键
【4月更文挑战第29天】本文探讨了.NET开发在构建智能应用中的关键作用,强调了其强大的框架、工具集、高效性能和跨平台支持。通过实例展示了.NET在人工智能、物联网及企业级应用中的应用。同时,指出了.NET开发面临的挑战,如技术更新的学习成本、性能优化、资源管理和安全隐私保护,并提出了应对策略。随着技术进步,.NET将在智能应用领域发挥更大作用,推动创新与便利。
|
5小时前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
22 0