背景
本文档介绍在unbuntu环境下如何安装ICE,并用C++写一个ICE应用
ICE简介
ICE是ZEROC的开源通信协议产品,它的全称是:The Internet Communications Engine,翻译为中文是互联网通信引擎,是一个流行的面向对象的工具包,使我们能够以最小的代价构建分布式应用程序。ICE使我们专注于应用逻辑的开发,它来处理所有底层的网络接口编程,这样我们就不用去考虑这样的细节:打开网络连接、网络数据传输的序列化与反序列化、连接失败的尝试次数等。
第一部分
下载
源码下载链接:戳这里
编译安装
tar zxvf ice-3.6.0.tar.gz
cd ice-3.6.0/cpp
make
make install
所遇到的问题&解决方法
1)ConnectionI.cpp:28:19: error: bzlib.h: 没有那个文件或目录
sudo apt-get install libmcpp-dev
2)../Freeze/TransactionI.h:15:20: error: db_cxx.h: 没有那个文件或目录
安装Berkeley DB,具体可参考:戳这里
BDB环境配置:
export DB_HOME=/usr/local/BerkeleyDB.4.6
PATH=$PATH:$DB_HOME/bin
. .bashrc
第二部分
1.创建ICE文件Printer.ice
module Demo { interface Printer { void printString(string s); }; };
2.Compiling a Slice Definition for C++
$ slice2cpp Printer.ice
3.创建Server.cpp
#include <Ice/Ice.h> #include <Printer.h> using namespace std; using namespace Demo; class PrinterI : public Printer { public: virtual void printString(const string& s, const Ice::Current&); }; void PrinterI:: printString(const string& s, const Ice::Current&) { cout << s << endl; } int main(int argc, char* argv[]) { int status = 0; Ice::CommunicatorPtr ic; try { ic = Ice::initialize(argc, argv); Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000"); Ice::ObjectPtr object = new PrinterI; adapter->add(object, ic->stringToIdentity("SimplePrinter")); adapter->activate(); ic->waitForShutdown(); } catch (const Ice::Exception& e) { cerr << e << endl; status = 1; } catch (const char* msg) { cerr << msg << endl; status = 1; } if (ic) { try { ic->destroy(); } catch (const Ice::Exception& e) { cerr << e << endl; status = 1; } } return status; }
4.创建Client.cpp
#include <Ice/Ice.h> #include <Printer.h> using namespace std; using namespace Demo; int main(int argc, char* argv[]) { int status = 0; Ice::CommunicatorPtr ic; try { ic = Ice::initialize(argc, argv); Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000"); PrinterPrx printer = PrinterPrx::checkedCast(base); if (!printer) throw "Invalid proxy"; printer->printString("Hello World!"); } catch (const Ice::Exception& ex) { cerr << ex << endl; status = 1; } catch (const char* msg) { cerr << msg << endl; status = 1; } if (ic) ic->destroy(); return status; }
5.测试
1.环境配置
export ICE_HOME=/opt/Ice-3.6.0
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DB_HOME/lib64:/home/bran/ice-3.6.0/cpp
$ c++ -I. -I$ICE_HOME/include -c Printer.cpp Server.cpp
$ c++ -o server Printer.o Server.o -L$ICE_HOME/lib -lIce -lIceUtil
$ c++ -I. -I$ICE_HOME/include -c Printer.cpp Client.cpp
$ c++ -o client Printer.o Client.o -L$ICE_HOME/lib -lIce -lIceUtil