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

原文地址:https://www.cnblogs.com/hx214blog/p/3116296.html