• 说说Remoting


    Remoting的认识和学习要感谢张逸老师,看了他的blog后,使我对Remoting技术有了新的认识,下面总结一下.

    remoting实现的结构,分为服务端,远程服务对象和客户端,看图

    image

    Remoting是一个分布式处理服务。服务器端首先创建通道(Channel),并自动开启监听通道。
    根据客户端发出的请求,传递远程对象。因此,编写Remoting程序,主要分为三部分:
         1、被传递的远程对象;
        2、服务器端监听程序;
       3、客户端请求和处理对象程序;
      一、被传递的远程对象
        在Remoting中,被传递的远程对象类是有诸多限制的。首先,我们必须清楚,这里所谓的传递是以引用的方式,因此所传递的远程对象类必须继承MarshalByRefObject。
       MarshalByRefObject 是那些通过使用代理交换消息来跨越应用程序域边界进行通信的对象的基类。不是从 MarshalByRefObject 继承的对象会以隐式方式按值封送。
        当远程应用程序引用一个按值封送的对象时,将跨越远程处理边界传递该对象的副本。因为您希望使用代理方法而不是副本方法进行通信,因此需要继承 MarshallByRefObject。(MSDN)
        这个类只实现了最简单的方法,就是设置一个人的基本信息,并返回一个Person类对象。值得注意的是,这里返回的Person类。由于是以引用和远程调用的方式。
        这里所传递的Person则是以传值的方式来完成。因此必须涉及到一个序列化的问题。所以,Remoting要求对象类还要调用或传递某个对象,例如类,或者结构,
      则该类或结构则必须实现串行化Attribute。[Serializable]。

    下面我们来做一个例子:

    远程对象代码:

         /// <summary>
        /// 人物
        /// </summary>
        [Serializable]
        public class Person
        {
            public string Name { get; set; }
            public string Sex { get; set; }
            public int Age { get; set; }
        }

    namespace RemoteObject
    {
        public interface IServerObject
        {
            Person GetPersonInfo(string name, string sex, int age);
        }
     
        public interface IServerObjFactory
        {
            IServerObject CreateInstance();
        }
     
        public class ServerObject : MarshalByRefObject, IServerObject
        {
            public Person GetPersonInfo(string name, string sex, int age)
            {
                Person person = new Person
                {
                    Name = name,
                    Sex = sex,
                    Age = age,
                };
                return person;
            }
        }
     
        public class ServerObjFactory : MarshalByRefObject, IServerObjFactory
        {
            public IServerObject CreateInstance()
            {
                return new ServerObject();
            }
        }
     
     
    }
    服务端监听程序代码:使用的是console程序
     
     class Server
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                //创建通道,使用8080端口;
                TcpChannel channel = new TcpChannel(8080);
     
                //注册通道;
                ChannelServices.RegisterChannel(channel);
     
                //传递对象;
                RemotingConfiguration.RegisterWellKnownServiceType(
                    typeof(RemoteObject.ServerObject),
                    "ServiceMessage", WellKnownObjectMode.SingleCall);
     
                Console.WriteLine("Open the server listener");
                Console.ReadLine();
            }
        }

    客户端请求代码:

     class Program
        {
            static void Main(string[] args)
            {
                TcpChannel channel = new TcpChannel();
                ChannelServices.RegisterChannel(channel);
     
                RemoteObject.IServerObject serverObj = (RemoteObject.IServerObject)
                    Activator.GetObject(
                    typeof(RemoteObject.IServerObject),
                    "tcp://localhost:8080/ServiceMessage");
     
                Console.WriteLine("Invoke remoting object:");
                RemoteObject.Person person = serverObj.GetPersonInfo("wayfarer", "male", 28);
     
                Console.WriteLine("name:{0},sex:{1},age:{2}", person.Name, person.Sex, person.Age);
                Console.ReadLine();
            }
        }

    OK,现在一个最简单的remoting程序就做好了!

  • 相关阅读:
    Java常量初始化后不会再去重新获取
    Intellij IDEA自动编译问题
    Tomcat关闭日志输出
    MySQL命令行导出数据库
    补充Mysql5.7用法
    Linux下安装 mysql 5.7
    IE9 表格错位bug
    Ubuntu忘记管理员密码
    实体转换计算器
    js生成二维码参数设置
  • 原文地址:https://www.cnblogs.com/lori/p/2098969.html
Copyright © 2020-2023  润新知