luck_mylife

导航

 

NET Framework 远程处理基础结构提供下列信道实现:

  • IpcChannel
  • TcpChannel
  • HttpChanne

IpcChannel

IPCChannel是.NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。在同一计算机上的应用程序域之间进行通信时,IPC 信道比 TCP 或 HTTP 信道要快得多。但是IPC只在本机应用之间通信。所以,在客户端和服务端在同一台机器时,我们可以通过注册IPCChannel来提高Remoting的性能。但如果客户端和服务端不在同一台机器时,我们不能注册IPCChannel。

IpcChannel 执行下列功能:

  • 使用命名管道在发送方和接收方之间通信。
  • 支持以二进制格式和行业标准 SOAP 序列化格式编码负载。
  • 生成并使用对象引用的 ChannelDataStore。
  • 支持模拟和委托。
  • 支持在命名管道上利用访问控制列表 (ACL) 来提供高级访问控制。

TcpChannel

TcpChannel 类使用二进制格式化程序将所有消息序列化为二进制流,并使用 TCP 协议将该流传输至目标统一资源标识符 (URI)。

TcpChannel 执行下列功能:

  • 使用 TCP 套接字在发送方和接收方之间通信。
  • 支持以二进制格式和行业标准 SOAP 序列化格式编码负载。
  • 生成并使用对象引用的 ChannelDataStore。
  • 支持模拟和委托。
  • 支持 SSPI 加密。

HttpChannel

HttpChannel 类使用 SOAP 协议在远程对象之间传输消息。所有消息都通过 SoapFormatter 传递,此格式化程序会将消息转换为 XML 并进行序列化,同时向数据流中添加所需的 SOAP 标头。如果还指定了二进制格式化程序,则会创建二进制数据流。随后,将使用 HTTP 协议将数据流传输至目标 URI。

HttpChannel 符合 SOAP 1.1 标准,它执行下列功能:

  • 通过将 HTTP 协议用作传输在发送方和接收方之间通信。
  • 支持以 SOAP(一种 XML 编码标准)和二进制格式编码负载。
  • 将接收方设置为通过 ASP.NET 和 TCP 套接字接收 HTTP 请求并发送 HTTP 响应。
  • 生成并使用对象引用的 ChannelDataStore。
  • 支持模拟和委托。
  • 支持 SSPI 加密。

 

1.远程处理类

using System;

namespace RemotingMarshal
{
    public class MessageMarshal : MarshalByRefObject
    {
        public void SetMes(string messge)
        {
            Console.WriteLine(messge);
        }
    }
}

 

2.服务端

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

namespace RemotingService
{
    public class UsersServices
    {
        static void Main(string[] args)
        {
            //TcpChannel channel = new TcpChannel(8090);
            ///*注册通道服务端*/
            //ChannelServices.RegisterChannel(channel, false);
            //RemotingConfiguration.ApplicationName = "Test";
            //RemotingConfiguration.RegisterActivatedServiceType(typeof(MessageMarshal));   

            //1.Tcp:
            TcpChannel channel = new TcpChannel(8090); //端口随便取

            //2.Http:
            //HttpChannel channel = new HttpChannel(8090);

            ChannelServices.RegisterChannel(channel, false);

            //注册远程对象
            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(MessageMarshal),
                "Test",
                WellKnownObjectMode.SingleCall);
            Console.Read();
        }
    }
}

 

3.客户端

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

namespace RemotingClient
{
    public class UsersClient
    {
        static void Main()
        {
            try
            {
                //TcpChannel channel = new TcpChannel();
                //ChannelServices.RegisterChannel(channel, false);
                //RemotingConfiguration.RegisterActivatedClientType(typeof(MessageMarshal), "TCP://localhost:8090/Test");
                //MessageMarshal mMarshal = new MessageMarshal();

                //1.Tcp
                MessageMarshal mMarshal = (MessageMarshal)Activator.GetObject(typeof(MessageMarshal), "TCP://localhost:8090/Test");

                //2.Http
                //MessageMarshal mMarshal = (MessageMarshal)Activator.GetObject(typeof(MessageMarshal), "http://localhost:8090/Test");

                int count = 0;
                while (true)
                {
                    mMarshal.SetMes("num:" + count++);
                    System.Threading.Thread.Sleep(1000);
                }
            }
            catch (Exception er)
            {
                Console.WriteLine(er.Message);
            }
            Console.Read();
        }
    }
}

 

posted on 2016-11-04 11:34  二郎神  阅读(575)  评论(0编辑  收藏  举报