zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

Net应用通信 信息 返回 发送 Remoting
2023-09-27 14:28:14 时间

两个界面如图:



代码:

服务端

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);
            }
        }
    }
}
 


【C#】.net core2.1,通过扩展状态代码页方法对404页面进行全局捕抓并响应信息 在开发一个网站项目时,除了异常过滤功能模块,还需要有针对404不存在的api接口和页面处理功能 本篇文章就来讲讲,如何自定义全局请求状态类来统一处理
.NET Core 获取操作系统各种信息 .NET Core 内置了一些API供我们获取操作系统、运行时、框架等信息。这些API不是很常用,所有有些小伙伴可能还不知道,这里做一些可能用到的获取操作系统的API介绍二.判断操作系统 判断操作系统是否为 Linux OSX Windows,主要使用 System.
阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:4.1 上报位置信息 阿里云物联网的位置服务,并不是完全独立的功能。位置信息包含 二维、三维,位置数据来源于属性的上传。 1)添加二维位置数据 打开 数据分析 - 空间数据可视化 - 二维数据 - 添加,为上面演示的设备添加位置,刷新时间为1秒。
.NET获取枚举DescriptionAttribute描述信息性能改进的多种方法 原文:.NET获取枚举DescriptionAttribute描述信息性能改进的多种方法 一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显示...
.NET Core2.1获取自定义配置文件信息   .net core来势已不可阻挡。既然挡不了,那我们就顺应它。了解它并学习它。今天我们就来看看和之前.net版本的配置文件读取方式有何异同,这里不在赘述.NET Core 基础知识。 ps:更新版,更新了多种方式实现读取配置文件信息,各位看官结合自己实际情况选择合适的读取方式即可。
[搬运] .NET Core 2.1中改进的堆栈信息 原文 : Stacktrace improvements in .NET Core 2.1 作者 : Ben Adams 译者 : 张很水 . NET Core 2.1 现在具有可读的异步堆栈信息!使得异步、迭代器和字典 ( key not found ) 中的堆栈更容易追踪! 这个大胆的主张意...