现在的位置: 首页 > 综合 > 正文

以按值传递的方法,调用远程对象的Remoting

2013年10月19日 ⁄ 综合 ⁄ 共 2652字 ⁄ 字号 评论关闭

(下面是从我自己讲义中摘取的一部分)

以按值传递的方法,调用远程对象的Remoting

DEMORemotingExam_2(2005)

 

我们也按上面说的三个步骤,来构建一个Remoting应用程序。

1、我们先建立一个类,做为服务器端和客户端的公用类:

代码如下:

namespace PubClass

{

 

    public class Pub_Class : MarshalByRefObject

    {

 

        public String returnYouInfo(PubClass.YouInfo1 tempinfo)

        {

            return tempinfo.Name + tempinfo.QQ;

        }

 

    }

   

 [Serializable]

    public class YouInfo1

    {

       string name="";

         string qq="";

        public YouInfo1(string name, string qq)

        {

            this.name = name;

            this.qq = qq;

        }

 

         public string Name

         {

              get{return name;}

              set{name = value;}

         }

 

         public string QQ

         {

              get{return qq;}

              set{qq = value;}

         }

 

       }

   

}

2、构建服务端程序:

服务端和刚才的例子的区别是:我们在这里,建立了一个控制台程序做为服务端。其中的代码和第一个例子差不多。

全部代码如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Remoting.Channels.Tcp;

using System.Runtime.Remoting.Channels.Http;

using System.Runtime.Remoting.Channels;

 

namespace RemotingExam_2_Server

{

    class Program

    {

        static void Main(string[] args)

        {

           

             TcpChannel chan1 = new TcpChannel(5000);

             HttpChannel chan2 = new HttpChannel(5001);

             ChannelServices.RegisterChannel(chan1, false);

             ChannelServices.RegisterChannel(chan2, false);

            System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType                (typeof(PubClass.Pub_Class),"MyServer",System.Runtime.Remoting.WellKnownObjectMode.Singleton);          

            System.Console.WriteLine("Remoting 服务启动成功....");

            System.Console.WriteLine("TCP端口号:5000");

            System.Console.WriteLine("HTTP端口号:5001");

            System.Console.ReadLine();

           

        }

    }   

}

3、构建客户端程序:

调用远程可序列化的对象

private void button3_Click(object sender, EventArgs e)

        {

            //使用TCP通道得到远程对象

            TcpChannel chan1 = new TcpChannel();

            //Activator.CreateInstance

            System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chan1, false);

            PubClass.Pub_Class obj1 = (PubClass.Pub_Class)Activator.GetObject(typeof(PubClass.Pub_Class), "tcp://localhost:5000/MyServer");

 

            if (obj1 == null)

            {

                this.textBox1.Text = "连接服务器失败...";

                return;

            }

            this.textBox1.Text = obj1.returnYouInfo(new PubClass.YouInfo1("东方钟", "276341866"));

            System.Runtime.Remoting.Channels.ChannelServices.UnregisterChannel(chan1);

        }

 

 

 

在这个类中,和上面的一样,有一个继承自MarshalByRefObject的类,说明这个类是可以被远程访问的对象。但在这个类的returnYouInfo方法中,有一个参数是另一个类(YouInfo1)的实例。也说是说:我们要在客户端调用这个方法,就要给这个方法传入一个YouInfo1类的实例才行。所以,要在客户端调用YouInfo1,我们就在这个类的前面加上[Serializable],表明这个类是可序列化的。这样,这个类才能被客户端调用。

抱歉!评论已关闭.