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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值