一个最简单的.NET Remoting构建的分布式应用程序示例

服务器端代码
文件名:Server.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace RemotingSamples
{

    public class Server
 {
  public static int Main(string [] args)
  {

   TcpChannel chan1 = new TcpChannel(8085);
   HttpChannel chan2 = new HttpChannel(8086);

   ChannelServices.RegisterChannel(chan1);
   ChannelServices.RegisterChannel(chan2);


            RemotingConfiguration.RegisterWellKnownServiceType
                (
                typeof(OrderNumber),
                "SayHello",
                WellKnownObjectMode.Singleton
                );
   

   System.Console.WriteLine("Press Enter key to exit");
   System.Console.ReadLine();
   return 0;
  }

 }
}

远程对象代码
文件名:OrderNumber.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace RemotingSamples
{
    public class OrderNumber : MarshalByRefObject
    {
        private int djIncNumber;
        public OrderNumber()
        {
            System.Console.WriteLine("服务器端已经被激活……");
        }
        public int IncOrderNumber()
        {
            djIncNumber = djIncNumber + 1;
            Console.WriteLine("服务被客户端调用{0}次", djIncNumber);
            return djIncNumber;
        }
    }
}

客户端代码
文件名:Client.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;

namespace RemotingSamples
{
 public class Client
 {
        public static void Main(string[] args)
        {
            //使用TCP通道得到远程对象
            TcpChannel chan1 = new TcpChannel();
            ChannelServices.RegisterChannel(chan1);
            OrderNumber obj1 = (OrderNumber)Activator.GetObject(
                typeof(RemotingSamples.OrderNumber),
                "tcp://localhost:8085/SayHello");
            if (obj1 == null)
            {
                System.Console.WriteLine(
                    "Could not locate TCP server");
                System.Console.ReadLine();
            }
            //使用HTTP通道得到远程对象
            HttpChannel chan2 = new HttpChannel();
            ChannelServices.RegisterChannel(chan2);
            OrderNumber obj2 = (OrderNumber)Activator.GetObject(
                typeof(RemotingSamples.OrderNumber),
                "http://localhost:8086/SayHello");
            if (obj2 == null)
            {
                System.Console.WriteLine(
                    "Could not locate HTTP server");
                System.Console.ReadLine();
            }

            Console.WriteLine(
                "Client1 TCP HelloMethod {0}",
                obj1.IncOrderNumber());
            Console.WriteLine(
                "Client2 HTTP HelloMethod {0}",
                obj2.IncOrderNumber());
            Console.ReadLine();
        }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值