您是第 Web Page Tracking 位访客

水~墨~

昂首阔步,不留一点遗憾!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Remoting

 

使用TCP/IP 协议,服务端可以是服务,web服务器,类。

 

例子1.  远程调用服务端的类,就像调用客户端机器上的类一样。

 

服务端代码 (先定义被客户端调用的类,然后注册到某个端口中去,客户端访问刚才注册地址  ip:端口号/类名)

 

1)类

类实现加法运算

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

namespace ConsoleApplication1
{
    public class RemotingSample:MarshalByRefObject
    {
        public RemotingSample()
        {
            Console.WriteLine("New References Added");
        }

        public int sum(int a, int b)
        {
            return a + b;
        }

    }
}

 

注:MarshalByRefObject   允许在支持远程处理的应用程序中跨应用程序域边界访问对象

 

2)服务端控制端应用程序

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

using System.Runtime.Remoting.Channels.Tcp;           //引用中必须加入 System.Runtime.Remoting 才能使用
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpServerChannel chanel = new TcpServerChannel(6666);
            ChannelServices.RegisterChannel(chanel);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingSample), "RemotingSample", WellKnownObjectMode.SingleCall);
            Console.WriteLine("请按下任意键");

            Console.ReadKey();
        }
    }
}

 

 

3)客户端。控制端应用程序

 

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

using System.Runtime;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using ConsoleApplication1;
namespace Rclient
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new TcpClientChannel(),true);   //本机调用true,false都可以

            RemotingSample remoteObj = (RemotingSample)(Activator.GetObject(typeof(RemotingSample), "tcp://localhost:6666/RemotingSample"));

            Console.WriteLine("1+2="+remoteObj.sum(1,2).ToString());
            Console.ReadKey();
        }
    }
}

 

 

 

 

开始调用,首先启动服务端程序实现注册端口和服务

image

 

客户端开始调用远程的类

image

查看本地端口,6668服务端口,22034是客户端主机随便分配的端口号。

image

 

 

 

总结:

1.使用下面的命名空间

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

 

2.

注册TCP端口供客户端调用,且一个协议只能注册一个

TcpServerChannel chanel = new TcpServerChannel(6668);  ///服务端注册tcp端口
ChannelServices.RegisterChannel(chanel);

接着注册Remoting服务

RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingSample), "RemotingSample", WellKnownObjectMode.SingleCall);

3. 客户端使用

调用TCP://ip:服务端注册的端口号/服务端注册的类

Console.WriteLine("1+2="+remoteObj.sum(1,2).ToString());

posted on 2014-02-19 22:27  水墨.MR.H  阅读(304)  评论(0编辑  收藏  举报
知识共享许可协议
本博客为水墨原创,基于Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的水墨(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。