.NET Remoting study notes (3) channel

Original: .NET Remoting Study Notes (3) Channel

content

 

Reference: ♂Windmill.Net

 

The .NET Framework remoting infrastructure provides the following channel implementations:

  • IpcChannel
  • TcpChannel
  • HttpChannel

IpcChannel

New in the .NET Framework 2.0, IPCChannel uses the Windows Interprocess Communication (IPC) system to transfer messages between application domains on the same computer. IPC channels are much faster than TCP or HTTP channels when communicating between application domains on the same computer. But IPC only communicates between native applications. Therefore, when the client and server are on the same machine, we can improve the performance of Remoting by registering IPCChannel. But if the client and server are not on the same machine, we cannot register IPCChannel .

IpcChannel performs the following functions:

  • Use named pipes to communicate between sender and receiver.
  • Supports encoding payloads in binary format and industry standard SOAP serialization format.
  • Generate and use a ChannelDataStore of object references.
  • Mocking and delegation are supported.
  • Supports the use of access control lists (ACLs) on named pipes to provide advanced access control.

 

TcpChannel

The TcpChannel class serializes all messages into a binary stream using a binary formatter and transmits the stream to a target Uniform Resource Identifier (URI) using the TCP protocol.

TcpChannel performs the following functions:

  • Use TCP sockets to communicate between sender and receiver.
  • Supports encoding payloads in binary format and industry standard SOAP serialization format.
  • Generate and use a ChannelDataStore of object references.
  • Mocking and delegation are supported.
  • SSPI encryption is supported.

 

HttpChannel

The HttpChannel class uses the SOAP protocol to transfer messages between remote objects. All messages are passed through the SoapFormatter, which converts and serializes the messages to XML, adding the required SOAP headers to the data stream. If a binary formatter is also specified, a binary data stream is created. The data is then streamed to the target URI using the HTTP protocol.

HttpChannel is SOAP 1.1 compliant and performs the following functions:

  • Communicate between sender and receiver by using the HTTP protocol as the transport.
  • Supports encoding payloads in SOAP (an XML encoding standard) and binary format.
  • Set up the receiver to receive HTTP requests and send HTTP responses over ASP.NET and TCP sockets.
  • Generate and use a ChannelDataStore of object references.
  • Mocking and delegation are supported.
  • SSPI encryption is supported.

 

Paste the code below:

1. Define the remote object

using System;
using System.Runtime.Remoting.Metadata;

/* code Sakyamuni */ 
namespace MessageMarshal
{
    /* Create a send message delegate */ 
    public  delegate  void SendMessageHandler( string messge);
    [Serializable]
    public class TestMessageMarshal : MarshalByRefObject
    {
        private Guid ID { get ; set ; }
         /* Recreate the ID number when creating a new object instance */ 
        public TestMessageMarshal()
        {
            ID = Guid.NewGuid();
        }

        /* Create send message event */ 
        public  static  event SendMessageHandler SendMessageEvent;

        /*发送消息*/
        [SoapMethod(XmlNamespace = "MessageMarshal", SoapAction = "MessageMarshal#SendMessage")]
        public void SendMessage(string messge)
        {
            if (SendMessageEvent != null)
                SendMessageEvent(ID.ToString() + "\t" + messge);
        }
    }
}

2. Define the server

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using MessageMarshal;

namespace TestRemotingServer
{
    /* code: Sakyamuni */  
    class Program
    {
        static void Main(string[] args)
        {
            //IpcChannel channel_ipc = new IpcChannel("localhost:8226");
            //HttpChannel channel_http = new HttpChannel(8226);
            TcpChannel channel_tcp = new TcpChannel(8226);

            /* Register the channel server */ 
            ChannelServices.RegisterChannel(channel_tcp, false );
            RemotingConfiguration.ApplicationName = "test";
            RemotingConfiguration.RegisterActivatedServiceType(typeof(TestMessageMarshal));   
            Console.WriteLine("started ..."); 
            /*接收客户端事件*/ 
            TestMessageMarshal.SendMessageEvent+=new SendMessageHandler(TestMessageMarshal_SendMessageEvent);
            Console.Read();
        }
         
        static void TestMessageMarshal_SendMessageEvent(string messge)
        {
            Console.WriteLine(messge);
        }
    }
}

3. Define the client:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using System.Threading;


/* code Sakyamuni */ 
namespace TestRemotingClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //IpcChannel channel = new IpcChannel();
            //HttpChannel channel_http = new HttpChannel();
            TcpChannel channel_tcp = new TcpChannel();
            ChannelServices.RegisterChannel(channel_tcp, false );
             /* Remoting type of registered channel */ 
            // RemotingConfiguration.RegisterActivatedClientType(typeof(MessageMarshal.TestMessageMarshal), "ipc: // localhost:8226/test");
             // RemotingConfiguration.RegisterActivatedClientType (typeof(MessageMarshal.TestMessageMarshal), " http://localhost :8226/test"); 
            RemotingConfiguration.RegisterActivatedClientType( typeof (MessageMarshal.TestMessageMarshal), " tcp://localhost:8226/test " );
             /* Create a message entity */
            MessageMarshal.TestMessageMarshal TestMessage = new MessageMarshal.TestMessageMarshal();
            while (true)
            {
                TestMessage.SendMessage("DateTime.Now:" + System.DateTime.Now.ToString());
                Console.WriteLine("send message...");
                Thread.Sleep(2000);
            }
        }
    }
}

4. Test

 

.NET Remoting We only write three articles, which should give you some understanding and deal with interviews

Author: Sakyamuni Sangha Source: http://www.cnblogs.com/woxpp/p/3997984.html The copyright of this article belongs to the author and the blog garden. Reprints are welcome, but this statement must be retained without the author's consent, and the The link to the original text is given in an obvious position on the article page.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325301886&siteId=291194637