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

  • 相关阅读:
    U盘禁用工具1.3
    《中文专业论文写作概论》笔记
    基于WAP1.0的手机论坛网站系统
    销售统计SQL
    移动如何保护个人开发者的合法权益?
    c#使用winnet检测网络连接状况
    HBASE客户端的一些参数设置
    代理模式
    使用Eclipse+Axis2生成webservice
    java多线程的理解
  • 原文地址:https://www.cnblogs.com/hx214blog/p/3116296.html
Copyright © 2011-2022 走看看