.Net中Remoting通信的应用,有发送和返回信息
两个界面如图:
代码:
服务端
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 ) 中的堆栈更容易追踪! 这个大胆的主张意...
相关文章
- 基于.NET CORE的后台管理系统-RuoYi
- RDIFramework.NET开发框架用户字典助力Saas数据字典应用
- RDIFramework.NET V3.3 WinForm版角色授权管理新增角色对操作权限项、模块起止生效日期的设置
- Asp.Net_MVC_防止HttpPost重复提交
- ASP.NET MVC项目中App_Code目录在程序应用
- Type 'Insus.NET.PictureObject' in Assembly 'App_Code, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
- WinForm(VB.NET)获取当前登录的用户名
- 关于 ASP.NET Web 应用中 async/await 注意问题
- 关于ADO.NET连接池的文章
- 字符串混淆技术在.NET程序保护中的应用及如何解密被混淆的字符串
- 如何在coding.net上进行Flask应用部署
- asp.net mvc 特性路由(MapMvcAttributeRoutes)的应用
- 提高 ASP.NET Web 应用性能的 24 种方法和技巧
- maven 加入json-lib.jar 报错 Missing artifact net.sf.json-lib:json-lib:jar:2.4:compile
- ASP.NET Core 集成 React SPA 应用
- ASP.NET Core Blazor Webassembly 之 渐进式应用(PWA)
- Net下几个服务框架介绍
- .NET应用架构设计—再次了解分层架构(现代企业应用分层架构核心设计元素)
- DevExpress ASP.NET and Blazor图表编制
- ASP.NET Core中的缓存[1]:如何在一个ASP.NET Core应用中使用缓存
- ASP.NET Core应用针对静态文件请求的处理[5]: DefaultFilesMiddleware中间件如何显示默认页面
- ASP.NET Core应用针对静态文件请求的处理[4]: DirectoryBrowserMiddleware中间件如何呈现目录结构
- 使用VS Code开发调试.NET Core 2.0
- Linux 部署ASP.NET SQLite 应用 的坎坷之旅 附demo及源码
- 一个在ASP.NET中利用服务器控件GridView实现数据增删改查的例子
- asp.net模版页面的高级应用