代码改变世界

多服务器Remoting

2013-06-04 00:02  Tellme_C#  阅读(745)  评论(0编辑  收藏  举报

================Server===============

using System;
using System.Runtime.Remoting;
using System.Collections;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace NET.MST.Twelfth.Server
{
    class Server1
    {
        /// <summary>
        /// 服务器端应用
        /// </summary>
        static void Main(string[] args)
        {
            String configfile = "Server.exe.config";
            RemotingConfiguration.Configure(configfile, true);
            Console.Read();
        }
    }
}

App.config 文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <channels>
        <channel ref="tcp" port="11111">
          <serverProviders>
            <provider ref="binary" typeFilterLevel="Full"/>
          </serverProviders>
        </channel>
      </channels>
      <service>
        <wellknown mode="Singleton"
                   type="NET.MST.Twelfth.SimpleAssembly.People, SimpleAssembly"
                   objectUri="MyServer1">
        </wellknown>
      </service>
    </application>
  </system.runtime.remoting>
</configuration>

===========Server2===============

using System;
using System.Runtime.Remoting;
using System.Collections;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters.Soap;
namespace NET.MST.Twelfth.Server
{
    class Server2
    {
        static void Main(string[] args)
        {
            String configfile = "Server2.exe.config";
            RemotingConfiguration.Configure(configfile, true);
            Console.Read();
        }
    }
}

App.config===============

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
      </service>
      <channels>
        <channel ref="tcp" port="22222">
          <serverProviders>
            <provider ref="binary" typeFilterLevel="Full"/>
          </serverProviders>
        </channel>
      </channels>
      <service>
        <wellknown mode="Singleton"
                   type="NET.MST.Twelfth.SimpleAssembly.PeopleManager, SimpleAssembly"
                   objectUri="MyServer2">
        </wellknown>
      </service>
    </application>
  </system.runtime.remoting>
</configuration>

===========Client================

using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using NET.MST.Twelfth.SimpleAssembly;
namespace NET.MST.Twelfth.Client
{
    class Client
    {
        /// <summary>
        /// 客户端应用
        /// </summary>
        static void Main(string[] args)
        {
            try
            {
                //使用TCP通道连接
                TcpClientChannel channel = new TcpClientChannel();
                ChannelServices.RegisterChannel(channel, true);
                ////获取服务器1的远程对象
                People people = (People)Activator.GetObject(
                    typeof(People), "tcp://localhost:11111/MyServer1");
                people.Name = "张三";
                people.Print();
                //现在访问服务器2的远程对象,并且以people为参数调用对象方法
                PeopleManager pm =(PeopleManager)Activator.GetObject(
                    typeof(PeopleManager),"tcp://localhost:22222/MyServer2");
                pm.PeopleGrow(people);
                people.Print();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.Read();
        }
    }
}

App.config===================

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <client>
        <wellknown type="NET.MST.Twelfth.SimpleAssembly.PeopleManager, SimpleAssembly"
                   url="tcp://localhost:22222/Server2">
        </wellknown>
      </client>
    </application>
  </system.runtime.remoting>
</configuration>

==========SimpleAssembly=============

using System;
namespace NET.MST.Twelfth.SimpleAssembly
{
    /// <summary>
    /// 远程对象类型
    /// </summary>
    public class People:MarshalByRefObject
    {
        private String _name;
        private int _age = 0;
        public People()
        {
        }
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        //打印对象
        public void Print()
        {
            Console.WriteLine(ToString());
        }
        //重写ToString方法
        public override string ToString()
        {
            return "对象名字为:" + _name +
                   " 对象年龄为:" + _age;
        }
        //年龄加1
        public void Grow()
        {
            _age++;
        }
    }
    /// <summary>
    /// 远程对象类型
    /// </summary>
    public class PeopleManager : MarshalByRefObject
    {
        public void PeopleGrow(People p)
        {
            p.Grow();
            Console.WriteLine("{0}长大一岁后:{1}", p.Name, p.ToString());
        }
    }
}