zoukankan      html  css  js  c++  java
  • c# remoting 小实例

    ////先定义接口

    using System;
    using System.Text;
    
    namespace IComm
    {
        /// <summary>
        /// send messages delegate
        /// </summary>
        /// <param name="Ms"></param>
        public delegate void SendEventHandler(string Ms);
        public interface ICom
        {
            /// <summary>
            /// send function
            /// </summary>
            /// <param name="Ms"></param>
            /// <returns></returns>
            void SendMs(string Ms);
        }
    }
    
    

    ////obj类

    using System;
    using System.Text;
    
    using IComm;
    
    namespace RemotingObj
    {
        public class UsersInfo:MarshalByRefObject,ICom
        {
            public static event SendEventHandler SendEventArgs;
            public void SendMs(string Ms)
            {
                if (SendEventArgs != null)
                    SendEventArgs(Ms);
            }
            public override object InitializeLifetimeService()
            {
                return null;
            }
        }
    }
    
    

    ////服务端代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    
    using IComm;
    using RemotingObj;
    
    
    namespace RemotingServer
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.toolStripStatusLabel1.ForeColor = Color.Red;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    TcpServerChannel server = new TcpServerChannel(1234);
                    ChannelServices.RegisterChannel(server, false);
                    RemotingConfiguration.RegisterWellKnownServiceType(typeof(UsersInfo), "abc", WellKnownObjectMode.SingleCall);
                    RemotingObj.UsersInfo.SendEventArgs += delegate(string s) { this.textBox1.Text = s; };
                    this.toolStripStatusLabel1.Text = "服务启动成功!";
                }
                catch (Exception ex) { this.toolStripStatusLabel1.Text = ex.Message; }
            }
        }
    }
    
    

    ///客户端

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    
    using IComm;
    using RemotingObj;
    
    namespace RemotingClient
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.toolStripStatusLabel1.ForeColor = Color.Red;
            }
            public ICom obj = null;
            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    ChannelServices.RegisterChannel(new TcpClientChannel(), false);
                    obj = (ICom)Activator.GetObject(typeof(ICom), "tcp://200.1.3.27:1234/abc");
                    if (obj != null)
                    {
                       
                        this.toolStripStatusLabel1.Text = "与服务器连接成功!";
                    }
                }
                catch (Exception ex) { this.toolStripStatusLabel1.Text = ex.Message; }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (obj != null && !string.IsNullOrEmpty(this.textBox1.Text))
                {
                    obj.SendMs(this.textBox1.Text);
                }
            }
        }
    }
    
    
  • 相关阅读:
    软阴影的实现(转帖)
    卡巴斯基:警惕IE拦截器恶意推广导航网站 狼人:
    安全问题拷问着电子支付第三方未来 狼人:
    微软警告:泄露的Office 2010预览版或含病毒 狼人:
    《越狱》完结 米帅迷应小心纹身网站挂马 狼人:
    赛门铁克和McAfee:目标锁定iPhone! 狼人:
    McAfee将与EMC合作推出在线PC备份服务 狼人:
    恶意软件分析师:面临社交网络威胁的用户已10亿 狼人:
    Mac OS X现漏洞 苹果称是Java导致恶意攻击 狼人:
    暴风影音声明:DNS服务器才是故障源头 狼人:
  • 原文地址:https://www.cnblogs.com/server126/p/1987258.html
Copyright © 2011-2022 走看看