简单Remoting例子

参考MSDN给出的例子

 

包含Remoting两种协议(tcp,http)的代码,其中在RemoteObject类添加数据访问的代码,则可实现NET Framwork间的简单分布式数据库程序。

 

 

1.服务端:

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 System.Runtime.Remoting.Channels.Http;
using System.Security.Permissions;
using RemotingModel;

namespace RemotingServer
{
    class Program
    {
        [SecurityPermission(SecurityAction.Demand)]
        static void Main(string[] args)
        {
            //TcpServer();

            HttpServer();
        }

        private static void TcpServer()
        {
            TcpChannel serverChannel = new TcpChannel(9090);
            ChannelServices.RegisterChannel(serverChannel, false);
            ChannelDataStore data = (ChannelDataStore)serverChannel.ChannelData;
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject.rem", WellKnownObjectMode.Singleton);

            Console.WriteLine("Press ENTER to exit the server.");
            Console.ReadLine();
        }

        private static void HttpServer()
        {
            HttpChannel serverChannel = new HttpChannel(9090);
            ChannelServices.RegisterChannel(serverChannel,false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject.rem", WellKnownObjectMode.Singleton);
           
            Console.WriteLine("Press ENTER to exit the server.");
            Console.ReadLine();
        }
    }
}

 

2.客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using RemotingModel;
using System.Runtime.Remoting.Messaging;

namespace RemotingClient
{
    class Program
    {
        [SecurityPermission(SecurityAction.Demand)]
        static void Main(string[] args)
        {
            //TcpClient();

            HttpClient();
        }

        private static void TcpClient()
        {
            TcpChannel clientChannel = new TcpChannel();
            ChannelServices.RegisterChannel(clientChannel,false);
            WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(RemoteObject), "tcp://localhost:9090/RemoteObject.rem");
            RemotingConfiguration.RegisterWellKnownClientType(remoteType);

            RemoteObject service = new RemoteObject();
            Console.WriteLine("The remote object has been called {0} times.", service.GetCount());
        }

        private static void HttpClient()
        {
            HttpClientChannel clientChannel = new HttpClientChannel();
            ChannelServices.RegisterChannel(clientChannel,false);
            WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(RemoteObject),"http://localhost:9090/RemoteObject.rem");
            RemotingConfiguration.RegisterWellKnownClientType(remoteType);
           
            RemoteObject service = new RemoteObject();
            Console.WriteLine("The remote object has been called {0} times.", service.GetCount());

        }

    }
}

 

3.对象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;

namespace RemotingModel
{
    public class RemoteObject : MarshalByRefObject
    {
        private int callCount = 0;

        public int GetCount()
        {
            callCount++;
            return (callCount);
        }
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值