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

  • 相关阅读:
    Dumpbin 工具的使用
    ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片
    directdraw显示yuv视频,出现屏保时,yuv显示不出来,表面丢失
    DirectX截图黑屏的解决办法
    VS2008 Project : error PRJ0019: 某个工具从以下位置返回了错误代码: "正在执行生成后事件..."解决方案
    RoundingMode 几个参数详解
    IDEA导入eclipse项目并部署运行完整步骤(转发)
    Intellij idea操作maven时控制台中文乱码
    java 替换json字符串中间的引号保留两边的引号,避免json校验失败
    分布式ID解决方案
  • 原文地址:https://www.cnblogs.com/hx214blog/p/3116296.html
Copyright © 2011-2022 走看看