您的位置:首页 > 其它

Ipc Channels in _Net framework remoting(2_0)

2006-09-04 11:45 543 查看
The most important one is the classes in the new System.Runtime.Remoting.Channels.Ipc namespace which allow applications on the same computer to communicate qucikly without using the network.As we known,In Framework 1.1,there are only two kinds of channel, Http and Tcp. And the speed of communiction between applications is the flaw of Remoting tech . With the joint of Ipc channel , at least, the communication between application domains on the same physical computer is much faster .

Here I only give the example of how to use Ipc Channel in C#. You can find details in 2005 documentation.

the example contains three parts:

A client,A server, A remoting object.

the following code shows a server

using System;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;

public class Server
{
[SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] args)
{
// Create the server channel.
IpcChannel serverChannel =
new IpcChannel("localhost:9090");

// Register the server channel.
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(
serverChannel);

// Show the name of the channel.
Console.WriteLine("The name of the channel is {0}.",
serverChannel.ChannelName);

// Show the priority of the channel.
Console.WriteLine("The priority of the channel is {0}.",
serverChannel.ChannelPriority);

// Show the URIs associated with the channel.
System.Runtime.Remoting.Channels.ChannelDataStore channelData =
(System.Runtime.Remoting.Channels.ChannelDataStore)
serverChannel.ChannelData;
foreach (string uri in channelData.ChannelUris)
{
Console.WriteLine("The channel URI is {0}.", uri);
}

// Expose an object for remote calls.
System.Runtime.Remoting.RemotingConfiguration.
RegisterWellKnownServiceType(
typeof(RemoteObject), "RemoteObject.rem",
System.Runtime.Remoting.WellKnownObjectMode.Singleton);

// Parse the channel's URI.
string[] urls = serverChannel.GetUrlsForUri("RemoteObject.rem");
if (urls.Length > 0)
{
string objectUrl = urls[0];
string objectUri;
string channelUri = serverChannel.Parse(objectUrl, out objectUri);
Console.WriteLine("The object URI is {0}.", objectUri);
Console.WriteLine("The channel URI is {0}.", channelUri);
Console.WriteLine("The object URL is {0}.", objectUrl);
}

// Wait for the user prompt.
Console.WriteLine("Press ENTER to exit the server.");
Console.ReadLine();
Console.WriteLine("The server is exiting.");
}
}

the following shows a client

using System;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;

public class Client
{
[SecurityPermission(SecurityAction.Demand)]
public static void Main(string[] args)
{
// Create the channel.
IpcChannel channel = new IpcChannel();

// Register the channel.
System.Runtime.Remoting.Channels.ChannelServices.
RegisterChannel(channel);

// Register as client for remote object.
System.Runtime.Remoting.WellKnownClientTypeEntry remoteType =
new System.Runtime.Remoting.WellKnownClientTypeEntry(
typeof(RemoteObject),
"ipc://localhost:9090/RemoteObject.rem");
System.Runtime.Remoting.RemotingConfiguration.
RegisterWellKnownClientType(remoteType);

// Create a message sink.
string objectUri;
System.Runtime.Remoting.Messaging.IMessageSink messageSink =
channel.CreateMessageSink(
"ipc://localhost:9090/RemoteObject.rem", null,
out objectUri);
Console.WriteLine("The URI of the message sink is {0}.",
objectUri);
if (messageSink != null)
{
Console.WriteLine("The type of the message sink is {0}.",
messageSink.GetType().ToString());
}

// Create an instance of the remote object.
RemoteObject service = new RemoteObject();

// Invoke a method on the remote object.
Console.WriteLine("The client is invoking the remote object.");
Console.WriteLine("The remote object has been called {0} times.",
service.GetCount());
}
}

the following shows the romoting object

using System;

// Remote object.
public class RemoteObject : MarshalByRefObject
{
private int callCount = 0;

public int GetCount()
{
Console.WriteLine("GetCount has been called.");
callCount++;
return(callCount);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐