图解C#中实现远程调用(Remoting)的示例代码(一)
2010-07-09 13:14
996 查看
先看下面的代码:
新建C/S间交互的数据类型(即远程对象类):
全部代码如下:
编译之。
新建服务端:
在引用中追加工程RemoteHello以及.net组件System.Runtime.Remoting。
代码如下:
在工程属性中选择.net framework 4.0版本。
保存并编译之。
建立客户端工程:
和服务端一样,在引用中追加工程RemoteHello以及.net组件System.Runtime.Remoting。
客户端代码如下:
依次运行服务端和客户端,结果如下:
服务端:
客户端:
注意:
1.关于信道:
Net平台下服务器数据通信,支持多信道如:http,tcp,icp,自定义信道
http:默认情况下80端口未被防火墙通过。
tcp: 必须配置防火墙,在内部网中使用更加有效。
IPc:最适合在单个系统上进行跨进程的通信,使用WINDOWS跨进程通信机制,速度最快。
2.关于远程对象类:
远程对象类必须由System.MarshalByRefObject派生出来的。
相关技术资料:
/article/5711776.html
新建C/S间交互的数据类型(即远程对象类):
全部代码如下:
#region Using directives using System; #endregion namespace Wrox.ProCSharp.Remoting { [Serializable] public class MySerialized { public MySerialized(int val) { a = val; } public void Foo() { Console.WriteLine("MySerialized.Foo called"); } public int A { get { Console.WriteLine("MySerialized.A called"); return a; } set { a = value; } } protected int a; } public class MyRemote : System.MarshalByRefObject { public MyRemote(int val) { a = val; } public void Foo() { Console.WriteLine("MyRemote.Foo called"); } public int A { get { Console.WriteLine("MyRemote.A called"); return a; } set { a = value; } } protected int a; } public class Hello : System.MarshalByRefObject { public Hello() { Console.WriteLine("Constructor called"); } ~Hello() { Console.WriteLine("Destructor called"); } public string Greeting(string name) { Console.WriteLine("Greeting called"); return "Hello, " + name; } public MySerialized GetMySerialized() { return new MySerialized(4711); } public MyRemote GetMyRemote() { return new MyRemote(4712); } } }
编译之。
新建服务端:
在引用中追加工程RemoteHello以及.net组件System.Runtime.Remoting。
代码如下:
#region Using directives using System; using System.Collections; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels.Ipc; #endregion namespace Wrox.ProCSharp.Remoting { class Program { static void ShowChannelProperties(IChannelReceiver channel) { Console.WriteLine("Name: " + channel.ChannelName); Console.WriteLine("Priority: " + channel.ChannelPriority); if (channel is HttpServerChannel) { HttpServerChannel httpChannel = channel as HttpServerChannel; Console.WriteLine("Scheme: " + httpChannel.ChannelScheme); } ChannelDataStore data = (ChannelDataStore)channel.ChannelData; foreach (string uri in data.ChannelUris) { Console.WriteLine("URI: " + uri); } Console.WriteLine(); } static void Main(string[] args) { // Create an TCP Channel TcpServerChannel tcpChannel = new TcpServerChannel(8086); ShowChannelProperties(tcpChannel); // Create an HTTP Channel with a Binary Formatter IDictionary properties = new Hashtable(); properties["name"] = "HTTP Channel with a Binary Formatter"; properties["priority"] = "15"; properties["port"] = "8085"; BinaryServerFormatterSinkProvider sinkProvider = new BinaryServerFormatterSinkProvider(); HttpServerChannel httpChannel = new HttpServerChannel( properties, sinkProvider); ShowChannelProperties(httpChannel); // Create an IPC Channel IpcServerChannel ipcChannel = new IpcServerChannel("myIPCPort"); ShowChannelProperties(ipcChannel); ChannelServices.RegisterChannel(tcpChannel); ChannelServices.RegisterChannel(httpChannel); ChannelServices.RegisterChannel(ipcChannel); RemotingConfiguration.RegisterWellKnownServiceType( typeof(Hello), "Hi", WellKnownObjectMode.SingleCall); System.Console.WriteLine("press return to exit"); System.Console.ReadLine(); } } }
在工程属性中选择.net framework 4.0版本。
保存并编译之。
建立客户端工程:
和服务端一样,在引用中追加工程RemoteHello以及.net组件System.Runtime.Remoting。
客户端代码如下:
#region Using directives using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Activation; #endregion namespace Wrox.ProCSharp.Remoting { class Program { static void Main(string[] args) { ChannelServices.RegisterChannel(new TcpClientChannel()); Hello obj = (Hello)Activator.GetObject( typeof(Hello), "tcp://localhost:8086/Hi"); // ChannelServices.RegisterChannel(new System.Runtime.Remoting.Channels.Ipc.IpcClientChannel()); // Hello obj = (Hello)Activator.GetObject( // typeof(Hello), "ipc://myIPCPort/Hi"); // Hello obj = (Hello)RemotingServices.Connect(typeof(Hello), // "tcp://localhost:8086/Hi"); // // object[] attrs = { new UrlAttribute("tcp://localhost:8086/HelloServer") }; // // ObjectHandle handle = Activator.CreateInstance( // "RemoteHello", "Wrox.ProCSharp.Remoting.Hello", attrs); // if (handle == null) // { // Console.WriteLine("could not locate server"); // return; // } // Hello obj = (Hello)handle.Unwrap(); // Console.WriteLine(obj.Greeting("Christian")); if (obj == null) { Console.WriteLine("could not locate server"); return; } MySerialized ser = obj.GetMySerialized(); if (!RemotingServices.IsTransparentProxy(ser)) { Console.WriteLine("ser is not a transparent proxy"); } ser.Foo(); MyRemote rem = obj.GetMyRemote(); if (RemotingServices.IsTransparentProxy(rem)) { Console.WriteLine("rem is a transparent proxy"); } rem.Foo(); // for (int i = 0; i < 5; i++) // { // Console.WriteLine(obj.Greeting("Christian")); // } Console.ReadLine(); } } }
依次运行服务端和客户端,结果如下:
服务端:
客户端:
注意:
1.关于信道:
Net平台下服务器数据通信,支持多信道如:http,tcp,icp,自定义信道
http:默认情况下80端口未被防火墙通过。
tcp: 必须配置防火墙,在内部网中使用更加有效。
IPc:最适合在单个系统上进行跨进程的通信,使用WINDOWS跨进程通信机制,速度最快。
2.关于远程对象类:
远程对象类必须由System.MarshalByRefObject派生出来的。
相关技术资料:
/article/5711776.html
相关文章推荐
- 图解C#中实现远程调用(Remoting)的示例代码(二)
- C#采用Remoting实现跨进程调用之代码实例
- C#实现使用HTTP管道的.NET远程代码示例
- COM方式实现C++调用C#代码的一些总结
- C#远程调用实现案例
- C#调用RDP,实现远程桌面共享及控制
- vs2010 c#调用mstscax.dll实现远程桌面连接
- C#实现通过程序自动抓取远程Web网页信息的代码
- C#实现文件上传下载Excel文档示例代码
- C#编程调用Cards.dll实现图形化发牌功能示例
- 飞信API(附带调用示例代码C#)
- c# 调用.bat文件的实现代码
- 贴下百度博客自动加好友的简单代码示例!!(c#.net实现)
- 用C#中实现的,调用CMD来执行BCP的代码
- 用c#中实现的,调用cmd来执行bcp的代码
- 利用c#实现远程注入非托管WIN32程序,并利用嵌入汇编调用非托管WIN32程序中的内部过程
- 使用PInvoke实现C#调用非托管C代码DLL库
- 代码示例:调用SPS提供的remoting服务,在线把Office文档转换成html文档
- C# 小规模网络远程调用的基类(基于Socket方式)实现
- 分布式编程->Remoting的一个代码示例(借助Remoting实现发送信息功能)