zoukankan      html  css  js  c++  java
  • Remoting 简单Demo

    ======RemoteServer=======

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using RemoteObject;

    namespace RemoteServer
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    TcpChannel chnl = new TcpChannel(8888);
                    ChannelServices.RegisterChannel(chnl, true);
                    RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("RemoteObject.RemoteObjClass,RemoteObject"), "Test", WellKnownObjectMode.Singleton);

                    Console.WriteLine("Server is running...");
                    Console.ReadLine();
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.ToString());
                    Console.ReadLine();
                }
            }
        }
    }

    ======RemoteClient========

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using RemoteObject;

    namespace RemoteClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    TcpClientChannel chnl = new TcpClientChannel();
                    ChannelServices.RegisterChannel(chnl, true);

                    while (true)
                    {
                        RemoteObjClass obj = (RemoteObjClass)
                        Activator.GetObject(Type.GetType("RemoteObject.RemoteObjClass,RemoteObject"), "tcp://localhost:8888/Test");
                        Console.WriteLine(obj.Multi(3,6));
                        Console.ReadLine();
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.ToString());
                    Console.ReadLine();
                }
            
            }
        }
    }

    ======RemoteObject=======

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace RemoteObject
    {
        public class RemoteObjClass : System.MarshalByRefObject
        {
            Person p = new Person();
            int i = 0;

            public String DisplayMessage()
            {
                return "HelloWorld" + i++;
            }

            public int Multi(int a, int b)
            {
                return a * b;
            }

            public void SetName(String name)
            {
                p.Name = name;
            }

            public String GetName()
            {
                return p.Name;
            }
          
            [Serializable]
            public class Person
            {
                public Person()
                {

                }

                private string name;
                private string sex;
                private int age;

                public string Name
                {
                    get { return name; }
                    set { name = value; }
                }

                public string Sex
                {
                    get { return sex; }
                    set { sex = value; }
                }

                public int Age
                {
                    get { return age; }
                    set { age = value; }
                }
            }
        }
    }

  • 相关阅读:
    CS224n笔记12 语音识别的end-to-end模型
    Vue组件
    关于网站
    Vue问题区
    数组的增、删、改、查
    Tree全部展开/折叠
    python 中UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)的解决方法
    python安装wxPython
    python中的迭代器和生成器
    python中的re正则表达式和模板系统
  • 原文地址:https://www.cnblogs.com/hx214blog/p/3116296.html
Copyright © 2011-2022 走看看