link:http://hi.baidu.com/dd_taiyangxue/blog/item/cc7f014442b4a82acffca382.html 一、准备条件 1、Eclipse3.4 2、Slice2Java插件:下载地址及使用说明:http://www.zeroc.com/eclipse.html,具体如下: ZeroC hosts an Eclipse plug-in site that you can add to your Eclipse configuration. Follow these steps to install the Slice2Java plug-in:
The plug-in requires Ice 3.3.1 or later. Users of Ice for Android 0.1 may also use the plug-in but you must upgrade your Ice installation to version 3.3.1 or later. 安装成功后:点击Eclipse的Window——preferences,如下图: 3、Ice-3.3.1-VC80.msi安装,目录为D:\Ice-3.3.1。下载地址:http://www.zeroc.com/download.html 二、操作步骤 1、建立Java工程:MyTestICE 2、建立slice文件夹,在其目录下建立:Printer.ice,内容如下: module PrinterInterface 3、使用Slice2Java插件(也可以使用命令行:slice2java Printer.ice) 右键点击工程“MyTestICE”——Slice2Java——Add Slice2Java builder,自动生成generated文件夹下的java文件,如下图所示:
4、编写servant类的PrinterI View Code
1 package printer; 2 3 import Ice.Current; 4 import PrinterInterface._PrinterDisp; 5 6 @SuppressWarnings("serial") 7 public class PrinterI extends _PrinterDisp 8 { 9 10 public void shutdown(Current arg0) 11 { 12 13 } 14 15 public void writeMessage(String arg0, int arg1, Current arg2) 16 { 17 18 } 19 20 public void printString(String s, Ice.Current current) 21 { 22 System.out.println("The string is : "+s); 23 } 24 25 } 5、编写ICE的server View Code
1 package server; 2 3 import printer.PrinterI; 4 5 public class Server 6 { 7 public static void main(String[] args) 8 { 9 int status = 0; 10 Ice.Communicator ic = null; 11 try 12 { 13 ic = Ice.Util.initialize(args); 14 Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints( 15 "SimplePrinterAdapter", "default -p 2888"); 16 Ice.Object object = new PrinterI(); 17 adapter.add(object, Ice.Util.stringToIdentity("SimplePrinter")); 18 adapter.activate(); 19 ic.waitForShutdown(); 20 } 21 catch (Ice.LocalException e) 22 { 23 e.printStackTrace(); 24 status = 1; 25 } 26 catch (Exception e) 27 { 28 System.err.println(e.getMessage()); 29 status = 1; 30 } 31 finally 32 { 33 if (ic != null) 34 ic.destroy(); 35 } 36 System.exit(status); 37 } 38 }
View Code
1 package client; 2 3 import PrinterInterface.PrinterPrx; 4 5 public class Client 6 { 7 public static void main(String[] args) 8 { 9 int status = 0; 10 Ice.Communicator ic = null; 11 try 12 { 13 ic = Ice.Util.initialize(args); 14 Ice.ObjectPrx base = ic 15 .stringToProxy("SimplePrinter:default -h 10.130.14.49 -p 2888"); 16 PrinterPrx printer = PrinterPrxHelper.checkedCast(base); 17 if (printer == null) 18 throw new Error("Invalid proxy"); 19 printer.printString("Hello World!"); 20 } 21 catch (Ice.LocalException e) 22 { 23 e.printStackTrace(); 24 status = 1; 25 } 26 catch (Exception e) 27 { 28 System.err.println(e.getMessage()); 29 status = 1; 30 } 31 finally 32 { 33 if (ic != null) 34 ic.destroy(); 35 } 36 System.exit(status); 37 } 38 }
7、在另外的机器上允许Server,在本机运行Client,在运行Server的机器上输出:The string is:Hello World! |