|
使用C#编写Zeroc ICE应用程序的例子 Internet Communications Engine (Ice) 是现代面向对象的中间件,它支持C++, C#, Java, Python, Ruby, PHP, 和 Visual Basic开发语言。Zeroc ICE中间件号称标准统一,开源,跨平台,跨语言,分布式,安全,服务透明,负载均衡,面向对象,性能优越,防火期穿透,通讯屏蔽。因此相比Corba,DCOM,SOAP,J2EE等的中间件技术,自然是集众多优点于一身,而且他简单易学。Ice是一个自由软件,所有代码都可用,是在GNU General Public License (GPL)下发布的。 这里.服务端和客户端都使用VS.NET 2005开发,语言采用C#,
1,安装ice,这里安装在D:\Lib\Ice-3.3.0 2,编写DemoIce.ice文件,
// ********************************************************************** // Copyright (c) 2003-2008 ZhangEF, Inc. All rights reserved. // **********************************************************************
#ifndef DEMO_ICE #define DEMO_ICE module Demo { enum DemoEnum { G722, G726, }; struct DemoStruct { int nValue; string strValue; DemoEnum eValue; }; interface DemoIce { idempotent void SetEx(int nId, DemoStruct param); ["cpp:const"] idempotent void GetEx(int nId, out DemoStruct param); }; }; //end module #endif
3,编译成DemoIce.cs文件 path D:\Lib\Ice-3.3.0\bin slice2cs.exe IceDemo.ice 4,创建.net2005 控制台项目添加现有项目 DemoIce.cs文件,添加引用 [ICE]\bin\Ice.dll 5,在服务断代码里实现抽象类的所有接口,抽象类在DemoIce.cs文件中,以Disp_结尾. 6,服务器端代码:using System; using System.Collections.Generic; using System.Text; using Demo; using System.Reflection;
namespace IceServerCon { public class DemoIceI : DemoIceDisp_ { DemoStruct m_data = new DemoStruct(); public override void GetEx(int nId, out DemoStruct param, Ice.Current current__) { param = m_data; Console.WriteLine("GetEx() called"); } public override void SetEx(int nId, DemoStruct param, Ice.Current current__) { m_data = param; Console.WriteLine("SetEx() called"); } }
class Program { static void Main(string[] args) { try { int status = 0; Ice.Communicator ic = null; try { ic = Ice.Util.initialize(ref args); Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("DemoIce", "default -p 10000"); Ice.Object obj = new DemoIceI(); adapter.add(obj, ic.stringToIdentity("DemoIce")); adapter.activate(); //ic.waitForShutdown(); } catch (Exception e) { Console.Error.WriteLine(e); status = 1; } Console.WriteLine("Server Start [OK]"); Console.ReadLine(); if (ic != null) { try { ic.destroy(); } catch (Exception e) { Console.Error.WriteLine(e); status = 1; } } if (status != 0) { System.Environment.Exit(status); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
7,客户端代码 using System; using System.Collections.Generic; using System.Text; using Demo; namespace IceClientCon { class Program { public static void Main(string[] args) { int status = 0; Ice.Communicator ic = null; try { ic = Ice.Util.initialize(ref args); Ice.ObjectPrx obj = ic.stringToProxy("DemoIce:tcp -h 127.0.0.1 -p 10000").ice_twoway().ice_timeout(200).ice_secure(false); DemoIcePrx demoIce = DemoIcePrxHelper.checkedCast(obj); if (demoIce == null) throw new ApplicationException("Invalid proxy"); DemoStruct data1 = new DemoStruct(); DemoStruct data2 ; data1.strValue = "Hellow Server"; demoIce.SetEx(1, data1); demoIce.GetEx(1, out data2); Console.WriteLine("data2.strValue = {0}", data2.strValue); } catch (Exception e) { Console.Error.WriteLine(e); status = 1; } Console.ReadLine(); if (ic != null) { try { ic.destroy(); } catch (Exception e) { Console.Error.WriteLine(e); status = 1; } } if (status != 0) { System.Environment.Exit(status); } } } }
|