• 【整理】delegate+RemotingServices 委托+远程调用的一个实例


    一、公共DLL库

    namespace RemoteSample
    {
    //客户端获取到服务端的对象实际是一个对象的引用,因此必须继承:MarshalByRefObject
    public class HelloServer : MarshalByRefObject
    {
    public delegate string MessageReceivedEvent(object sender, MessageReceivedEventArgs e);
    public event MessageReceivedEvent MessageReceived;

    public HelloServer()
    {
    Console.WriteLine(
    "HelloServer activated");
    }

    public String HelloMethod(String name)
    {
    return MessageReceived(this,new MessageReceivedEventArgs(name));
    }

    //说明1:在Remoting中的远程对象中,如果还要调用或传递某个对象,例如类,或者结构,则该类或结构则必须实现串行化Attribute[SerializableAttribute]:
    //说明2:将该远程对象以类库的方式编译成Dll。这个Dll将分别放在服务器端和客户端,以添加引用
    //说明3:在Remoting中能够传递的远程对象可以是各种类型,包括复杂的DataSet对象,只要它能够被序列化

    }

    public class MessageReceivedEventArgs : EventArgs
    {
    public string str = "";

    public MessageReceivedEventArgs(string str1)
    {
    this.str = str1;
    }
    }
    }

    二、Server

    【IP:192.168.142.1】

    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;

    namespace RemoteSample
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private ObjRef mi_Service = null;

    private void Form1_Load(object sender, EventArgs e)
    {
    TcpChannel chan
    = new TcpChannel(8085);
    ChannelServices.RegisterChannel(chan,
    false);
    string sshan = chan.ChannelName;
    listBox1.Items.Add(sshan);
    //注册远程对象,即激活.
    //RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteSample.HelloServer), "SayHello", WellKnownObjectMode.SingleCall);

    HelloServer mi_Transfer
    = new HelloServer();
    mi_Service
    = RemotingServices.Marshal(mi_Transfer, "SECSRemote");

    mi_Transfer.MessageReceived
    += new HelloServer.MessageReceivedEvent(OnNewMessage);
    }

    private string OnNewMessage(object sender,MessageReceivedEventArgs e)
    {
    return "Parameter : " + e.str ;
    }
    }
    }

    三、Client

    using System.Runtime.Remoting.Channels.Tcp;
    using System.Runtime.Remoting.Channels;

    namespace RemoteSample
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    TcpChannel chan
    = new TcpChannel();
    ChannelServices.RegisterChannel(chan);
    }

    private void button1_Click(object sender, EventArgs e)
    {
    HelloServer obj
    = (HelloServer)Activator.GetObject(typeof(HelloServer), "tcp://192.168.142.1:8085/SECSRemote");
    if (obj == null)
    MessageBox.Show(
    "Could not locate server");
    else
    MessageBox.Show(obj.HelloMethod(
    "Caveman"));
    }
    }
    }

    运行Server后,再打开Client,点击按钮,将弹出消息:Parameter:Caveman

  • 相关阅读:
    LeetCode(62)Unique Paths
    4_蒙特卡罗算法求圆周率PI
    CentOS安装sctp协议
    3_寻找假币问题(分治法)
    Gdb调试命令
    高阶函数 -------JavaScript
    方法 -------JavaScript
    函数定义和调用 -------JavaScript
    iterable -------JavaScript
    Map和Set -----JavaScript
  • 原文地址:https://www.cnblogs.com/wonder315/p/1963397.html
Copyright © 2020-2023  润新知