ICE 是 DCOM/CORBA 改进后的产物,接口描述语言Slice与IDL类似。 (一): 定义接口文件printer.ice module helloworld { interface Printer { void printString(string s); }; }; (二):服务器代 码 #include "stdafx.h" #include <ice/ice.h> #include <printer.h> class PrinterI : public helloworld::Printer { public: virtual void printString(const std::string& s, const Ice::Current&) { std::cout << "hello, world" << std::endl; } }; int _tmain(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) { std::cerr << e << std::endl; status = 1; } catch (const char* msg) { std::cerr << msg << std::endl; status = 1; } if (ic) ic->destroy(); return 0; } (三)客户端代码 #include "stdafx.h" #include <ice/Ice.h> #include <printer.h> int _tmain(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"); helloworld::PrinterPrx printer = helloworld::PrinterPrx::checkedCast(base); if (!printer) throw "Invalid proxy"; printer->printString("Hello, World"); } catch (const Ice::Exception& e) { std::cerr << e << std::endl; status = 1; } catch (const char* msg) { std::cerr << msg << std::endl; status = 1; } if (ic) ic->destroy(); return 0; }