ZeroC ICE 是指ZeroC公司的ICE(Internet Communications Engine)中间件平台。
Ice 应用适合于异构平台环境中使用:客户和服务器可以采用不同的编程语言,可以运行在不同的操作系统和机器架构上,并且可以使用多种网络技术进行通信。无论部署环境如何,这些应用的源码都是可移植的。ICE使我们专注于应用逻辑的开发,它来处理所有底层的网络接口编程,这样我们就不用去考虑这样的细节:打开网络连接、网络数据传输的序列化与反序列化、连接失败的尝试次数等。
--------------------------------------------------------------------------------------------------------------------------------------------------------
在编译ice时,会提示缺少哪些必要工具,根据提示依次安装即可。
1.安装gcc,g++
yum install gcc
yum install gcc-c++
2.安装设置mcpp
yum install mcpp.x86_64
ln -s /usr/lib64/libmcpp.so.0.3.0 /usr/lib/libmcpp.so
3.安装bzip
yum install bzip2-devel.x86_64
4.安装openssh
yum install openssl-devel.x86_64
5.安装expat
yum install expat-devel.x86_64
6.安装Berkeley DB
yum install db4-devel.x86_64
7.安装ice
源码下载:http://download.zeroc.com/Ice/3.5/Ice-3.5.1.tar.gz
主目录:make
进入cpp:make install
--------------------------------------------------------------------------------------------------------------------------------------------------------
demo.ice:定义服务器和客户端的公共接口
module Demo{ interface test{ string execute(string mth,string cmd); }; };
client.cpp:客户端程序
#include <string> #include <Ice/Ice.h> #include <demo.h> using namespace std; using namespace Demo; int main(void) { try { Ice::CommunicatorPtr ic; testPrx testServer; Ice::ObjectPrx base; ic = Ice::initialize(); base = ic->stringToProxy("TestAdapter:default -p 10000"); testServer = testPrx::checkedCast(base); string mystr = testServer->execute("My first cpp "," ice demo"); printf("result:%s\n",mystr.c_str()); } catch (const Ice::Exception& ex) { cerr << ex << endl; } }
server.cpp:服务端程序
#include <Ice/Ice.h> #include <demo.h> using namespace std; using namespace Demo; class Server:public test { public: ::std::string execute (const string & mth, const string & str,const Ice::Current &); public: Server (); }; Server::Server () { }; std::string Server::execute (const string & mth, const string & str,const Ice::Current &) { cout << mth + str << endl; return mth + str; } int main () { int status=0; Ice::CommunicatorPtr ic; try { ic = Ice::initialize(); Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints ("TestAdapter","default -p 10000"); Ice::ObjectPtr object = new Server; adapter->add (object, ic->stringToIdentity ("TestAdapter")); 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; }
原文地址:http://blog.csdn.net/zsp_skyer/article/details/41511903