11
2012
06

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

 



版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

相关文章:

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。