C# .Net Remoting示例
2010-05-10 11:07
387 查看
IPCChannel是.NET Framework 2.0 里面新增的,它使用 Windows 进程间通信 (IPC) 系统在同一计算机上的应用程序域之间传输消息。在同一计算机上的应用程序域之间进行通信时,IPC 信道比 TCP 或 HTTP 信道要快得多。但是IPC只在本机应用之间通信。所以,在客户端和服务端在同一台机器时,我们可以通过注册IPCChannel来提高Remoting的性能。但如果客户端和服务端不在同一台机器时,我们不能注册IPCChannel。
IPC(Inter-Process Communication,进程间通信)
/Files/wucg/_TestProjects/TestNetRemoting_IPC通信.rar
eg1:
server端
namespace DotNetRemotingDemo2Server
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("DotNetRemotingDemo2Server.exe.config",false);
Console.ReadLine();
}
}
}
配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<!--<wellknown type="RemoteHelloConsole.Hello,RemoteHelloConsole" objectUri="Object.MyObject" mode="Singleton" />-->
<wellknown type="RemoteHelloConsole.Hello,RemoteHelloConsole" objectUri="Object.MyObject" mode="SingleCall" />
</service>
<channels>
<channel ref="ipc" portName="testPipe" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
Client端:
namespace DotNetRemotingDemo2Client
{
class Program
{
static void Main(string[] args)
{
RemoteHelloConsole.Hello hello =
(RemoteHelloConsole.Hello)Activator.GetObject(typeof(RemoteHelloConsole.Hello),
"ipc://testPipe/Object.MyObject");
try
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(hello.Greeting("abc123" + i));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
eg2:
Server端:
namespace DotNetRemotingDemo1
{
class Program
{
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
//typeof(RemoteHelloConsole.Hello), "Hi", WellKnownObjectMode.SingleCall);
typeof(RemoteHelloConsole.Hello), "Hi", WellKnownObjectMode.Singleton);
Console.WriteLine("press return to exit.");
Console.ReadLine();
}
}
}
Client端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace DotNetRemotingClient
{
class Program
{
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
RemoteHelloConsole.Hello hello = (RemoteHelloConsole.Hello)Activator.GetObject(typeof(RemoteHelloConsole.Hello),
"tcp://localhost:8086/Hi");
if (hello == null) {
Console.WriteLine("can't locate server.");
return;
}
try
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine( hello.Greeting("abc123" + i) );
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
IPC(Inter-Process Communication,进程间通信)
/Files/wucg/_TestProjects/TestNetRemoting_IPC通信.rar
eg1:
server端
namespace DotNetRemotingDemo2Server
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("DotNetRemotingDemo2Server.exe.config",false);
Console.ReadLine();
}
}
}
配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<!--<wellknown type="RemoteHelloConsole.Hello,RemoteHelloConsole" objectUri="Object.MyObject" mode="Singleton" />-->
<wellknown type="RemoteHelloConsole.Hello,RemoteHelloConsole" objectUri="Object.MyObject" mode="SingleCall" />
</service>
<channels>
<channel ref="ipc" portName="testPipe" />
</channels>
</application>
</system.runtime.remoting>
</configuration>
Client端:
namespace DotNetRemotingDemo2Client
{
class Program
{
static void Main(string[] args)
{
RemoteHelloConsole.Hello hello =
(RemoteHelloConsole.Hello)Activator.GetObject(typeof(RemoteHelloConsole.Hello),
"ipc://testPipe/Object.MyObject");
try
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(hello.Greeting("abc123" + i));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
eg2:
Server端:
namespace DotNetRemotingDemo1
{
class Program
{
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
//typeof(RemoteHelloConsole.Hello), "Hi", WellKnownObjectMode.SingleCall);
typeof(RemoteHelloConsole.Hello), "Hi", WellKnownObjectMode.Singleton);
Console.WriteLine("press return to exit.");
Console.ReadLine();
}
}
}
Client端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace DotNetRemotingClient
{
class Program
{
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
RemoteHelloConsole.Hello hello = (RemoteHelloConsole.Hello)Activator.GetObject(typeof(RemoteHelloConsole.Hello),
"tcp://localhost:8086/Hi");
if (hello == null) {
Console.WriteLine("can't locate server.");
return;
}
try
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine( hello.Greeting("abc123" + i) );
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
相关文章推荐
- C# .NET Remoting 简单应用示例
- Remoting in .NET Using C#
- Asp.Net 3.5 应用Linq的例子1(C#)以及Web.Config示例
- C#使用SnmpSharpNet接收snmp的trap消息示例代码
- 企业号微信支付 公众号支付 H5调起支付API示例代码 JSSDK C# .NET
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- [导入]Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- 图解C#中实现远程调用(Remoting)的示例代码(一)
- c#使用Lucene.net创建索引,实现搜索的代码示例
- .net C# XML / Binary 序列化反序列化代码示例
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- asp.net(c#) static关键字用法小结+示例代码
- 转载:Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- .net Remoting(1)——起点,从示例做起
- C# .NET 开发框架EasyObjects及MyGeneration配置示例 (BY 冷家锋)
- ASP.NET C# 访问Oracle数据库示例
- asp.net C# 语言 Wait方式启动多个 进程的示例
- C# .net实现货币转换示例
- c#(asp.net) 多线程示例,用于同时处理多个任务