Catalogue
- . NET Remoting Learning Notes (i) concepts
- . NET Remoting Learning Notes (ii) how to activate
- . NET Remoting Learning Note (c) channel
Reference: ♂ Windmill. Net
The . NET Framework Remoting Infrastructure provides the following channel implementations:
- IpcChannel
- TcpChannel
- HttpChannel
IpcChannel
IpcChannel is new in the. NET Framework 2.0, which uses the Windows interprocess communication (IPC) system to transfer messages between application domains on the same computer. When communicating between application domains on the same computer, the IPC channel is much faster than the TCP or HTTP channel. But the IPC only communicates between native apps. So, when the client and the server are on the same machine, we can improve the performance of remoting by registering the IpcChannel. However , we cannot register IpcChannel if the client and the server are not on the same machine.
IpcChannel performs the following functions:
- Use Named pipes to communicate between the sender and the receiver.
- Supports encoding of loads in binary format and industry standard SOAP serialization format.
- Generates and uses the Channeldatastore of the object reference.
- Supports impersonation and delegation.
- Supports the use of access control lists (ACLs) on named Pipes to provide advanced access control.
TcpChannel
The TcpChannel class uses a binary formatter to serialize all messages to a binary stream and uses the TCP protocol to transfer the stream to the target Uniform Resource Identifier (URI).
TcpChannel performs the following functions:
- Use a TCP socket to communicate between the sender and the receiver.
- Supports encoding of loads in binary format and industry standard SOAP serialization format.
- Generates and uses the Channeldatastore of the object reference.
- Supports impersonation and delegation.
- SSPI encryption is supported.
HttpChannel
The HttpChannel class uses the SOAP protocol to transfer messages between remote objects. All messages are passed through SoapFormatter, which converts the message to XML and serializes it, adding the required SOAP headers to the data flow. If a binary formatter is also specified, a binary data stream is created. The data stream is then transmitted to the destination URI using the HTTP protocol.
HttpChannel conforms to the SOAP 1.1 standard, which performs the following functions:
- Communicates between the sender and receiver by using the HTTP protocol as a transport.
- Supports encoding load in SOAP (an XML encoding standard) and binary format.
- Set the receiver to receive HTTP requests via ASP. NET and TCP sockets and send HTTP responses.
- Generates and uses the Channeldatastore of the object reference.
- Supports impersonation and delegation.
- SSPI encryption is supported.
Paste the following code:
1. Defining Remote Objects
usingSystem;usingSystem.Runtime.Remoting.Metadata;/*code to release the bitter monk*/namespacemessagemarshal{/*Create a Send message delegate*/ Public Delegate voidSendmessagehandler (stringMessge); [Serializable] Public classTestmessagemarshal:marshalbyrefobject {PrivateGuid ID {Get;Set; } /*re-create the identification number when creating a new object instance*/ PublicTestmessagemarshal () {ID=Guid.NewGuid (); } /*Create a Send message event*/ Public Static EventSendmessagehandler sendmessageevent; /*Send Message*/[Soapmethod (XmlNamespace="Messagemarshal", SOAPAction ="Messagemarshal#sendmessage")] Public voidSendMessage (stringMessge) { if(Sendmessageevent! =NULL) sendmessageevent (ID. ToString ()+"\ t"+Messge); } }}
2. Define the service side
usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSystem.Runtime.Remoting.Channels.Http;usingSystem.Runtime.Remoting.Channels.Ipc;usingSYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;usingMessagemarshal;namespacetestremotingserver{/*code: The Buddha of the bitter monk*/ classProgram {Static voidMain (string[] args) { //IpcChannel CHANNEL_IPC = new IpcChannel ("localhost:8226"); //HttpChannel channel_http = new HttpChannel (8226);TcpChannel channel_tcp =NewTcpChannel (8226); /*Register channel service side*/ChannelServices.RegisterChannel (channel_tcp,false); Remotingconfiguration.applicationname="Test"; Remotingconfiguration.registeractivatedservicetype (typeof(Testmessagemarshal)); Console.WriteLine ("started ..."); /*Receiving client Events*/testmessagemarshal.sendmessageevent+=NewSendmessagehandler (testmessagemarshal_sendmessageevent); Console.read (); } Static voidTestmessagemarshal_sendmessageevent (stringMessge) {Console.WriteLine (MESSGE); } }}
3. Define the client:
usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSystem.Runtime.Remoting.Channels.Http;usingSystem.Runtime.Remoting.Channels.Ipc;usingSYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;usingSystem.Threading;/*code to release the bitter monk*/namespacetestremotingclient{classProgram {Static voidMain (string[] args) { //IpcChannel channel = new IpcChannel (); //HttpChannel channel_http = new HttpChannel ();TcpChannel channel_tcp =NewTcpChannel (); ChannelServices.RegisterChannel (CHANNEL_TCP,false); /*Remote processing type for registered channels*/ //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=NewMessagemarshal.testmessagemarshal (); while(true) {testmessage.sendmessage ("DateTime.Now:"+System.DateTime.Now.ToString ()); Console.WriteLine ("Send Message ..."); Thread.Sleep ( -); } } }}
4. Testing
The source of the Buddhist monk: http://www.cnblogs.com/woxpp/p/3997984.html This copyright belongs to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph, and in the article page obvious location to the original link.
. NET Remoting Learning Note (c) channel