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
#mcpp 2.7.2 with pat
OpenSSL#o#o. or later (on AIX and Linux)
安裝lmdb-devel mcpp-devel(其他函式庫系統可能自帶,可先不安裝,等make報錯再安裝)
sudo yum install lmdb-devel mcpp-devel
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
sudo yum install openssl-devel
src/Ice /ConnectionI.cpp:27:21: 致命錯誤:bzlib.h:沒有那個檔案或目錄sudo yum install bzip2-devel
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
#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; }
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
以上是linux下ice是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!