linux下的ice是“Internet Communications Engine”的简称,是互联网通信引擎的意思,是一个面向对象的中间件;ICE可以专注于应用逻辑的开发,用于处理所有底层的网络接口编程,可用最小的代价构建分布式应用程序。
本教程操作环境:linux7.3系统、Dell G3电脑。
ICE是ZEROC的开源通信协议产品,它的全称是:The Internet Communications Engine,翻译为中文是互联网通信引擎,是一个面向对象的中间件,使我们能够以最小的代价构建分布式应用程序。
ICE使我们专注于应用逻辑的开发,它来处理所有底层的网络接口编程,这样我们就不用去考虑这样的细节:打开网络连接、网络数据传输的序列化与反序列化、连接失败的尝试次数等。
示例在linux下安装ice过程:
下载安装源
cd /etc/yum.repos.d sudo wget https://zeroc.com/download/Ice/3.7/el7/zeroc-ice3.7.repo
Ice所需库
bzip2 1.0
expat 2.1 or later
LMDB 0.9 (LMDB is not required with the C++11 mapping)
mcpp 2.7.2 with patches
OpenSSL 1.0.0 or later (on AIX and Linux)
安装lmdb-devel mcpp-devel(其他库系统可能自带有,可先不安装,等make报错再安装)
sudo yum install lmdb-devel mcpp-devel
下载Ice源码ice-3.7.3.tar.gz,解压
cd /home/user/ tar -zxf ice-3.7.3.tar.gz cd ice-3.7.3/cpp
编译(默认C++98,可设置CXXFLAGS修改为c++11)
sudo make -j4 sudo make CXXFLAGS=-std=c++11 -j4
src/Ice/SHA1.cpp:14:31: 致命错误:openssl/sha.h:没有那个文件或目录
sudo yum install openssl-devel
src/Ice/ConnectionI.cpp:27:21: 致命错误:bzlib.h:没有那个文件或目录
sudo yum install bzip2-devel
src/IceXML/Parser.cpp:7:19: 致命错误:expat.h:没有那个文件或目录
sudo yum install expat-devel
安装(默认安装在/opt/Ice-3.7.3)
sudo make install
测试
1)编写ice的slice配置文件
module Demo { interface Printer { void printString(string s); }; };
2)编译Slice定义文件(生成Printer.h 和Printer.cpp)找不到slice2cpp,自己配置运行环境在/opt/Ice-3.7.3/bin中
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)编译服务端
c++ -I. -I/opt/Ice-3.7.3/include -c Printer.cpp Server.cpp c++ -o server Printer.o Server.o -L/opt/Ice-3.7.3/lib64 -lIce -lpthread
5)编写客户端,命名为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; }
6)编译客户端
c++ -I. -I/opt/Ice-3.7.3/include -c Printer.cpp Client.cpp c++ -o client Printer.o Client.o -L/opt/Ice-3.7.3/lib64 -lIce -lpthread
7)运行Server,然后运行Client,如果看到输出Hello World!则成功。
相关推荐:《Linux视频教程》
以上是linux下ice是什么的详细内容。更多信息请关注PHP中文网其他相关文章!