二 示例程序一
睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接
示例程序一
1)
ICE文件 Printer.ice
module demo
{
interface Printer
{
void printString(string s);
};
};
2)
Server端開發
選擇Win32控制臺空白項目 添加Iced.lib IceUtild.lib
增加Server.cpp,內容:
#include "Printer.h"
#include <Ice/Application.h>
#include <Ice/Ice.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; // ICE運行環境句柄
try
{
// 調用Ice::initialize,初始化Ice run time
// initialize 調用返回的是一個智能指針,指向一個Ice::Communicator對象,這個指針是Ice run time 的主句柄
ic = Ice::initialize(argc, argv);
// 調用Communicator 實例上的createObjectAdapterWithEndpoints,創建一個對象適配器。
// 是"SimplePrinterAdapter" (適配器的名字)
// "default -p 10000",后者是要適配器用缺省協議(TCP/IP)在端10000 處偵聽到來的請求。
Ice::ObjectAdapterPtr adapter= ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");
// 我們實例化一個PrinterI 對象,為我們的Printer 接口創建一個servant。
Ice::ObjectPtr object = new PrinterI;
// 調用適配器的add,告訴它有了一個新的servant ;傳給add 的參數是我們剛才實例化的servant,再加上一個標識符。
// 在這里,"SimplePrinter" 串是servant 的名字
adapter->add(object,ic->stringToIdentity("SimplePrinter"));
// 如果您按網上盛傳的文章寫成adapter->add(object,ICE::stringToIdentity("SimplePrinter"));您就死翹翹了
// 就這個問題纏了我3天,后來發現官方文檔是ic->stringToIdentity("SimplePrinter"),總算解圍了,否則還得死繞
// 調用適配器的activate 方法激活適配器 // (適配器一開始是在扣留(holding)狀態創建的;這種做法在下面這樣的情況下很有用: // 我們有多個servant,它們共享同一個適配器,而在所有servant實例化之前我們不想處理請求)。 // 一旦適配器被激活,服務器就會開始處理來自客戶的請求。 adapter->activate();
// 調用waitForShutdown。這個方法掛起發出調用的線程,直到服務器實現終止為止 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; } 3) Client端開發 選擇Win32控制臺空白項目 添加Iced.lib IceUtild.lib 增加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 { // 調用Ice::initialize 初始化Ice runtime。 ic = Ice::initialize(argc, argv); // 調用通信器的stringToProxy創建一個代理,所用參數是"SimplePrinter:default -p 10000"。 // 注意,這個串包含的是對象標識和服務器所用的端口號 Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:default -p 10000"); // stringToProxy 返回的代理的類型是Ice::ObjectPrx,這種類型位于接口和類的繼承樹的根部。 // 但要實際與我們的打印機交談,我們需要的是Printer 接口、而不是Object 接口的代理。 // 為此,我們需要調用PrinterPrx::checkedCast 進行向下轉換。這個方法會發送一條消息給服務器, // 實際詢問“這是Printer 接口的代理嗎?”如果是,這個調用就會返回Printer 的一個代理; // 如果代理代表的是其他類型的接口,這個調用就會返回一個空代理。 PrinterPrx printer = PrinterPrx::checkedCast(base); // 測試向下轉換是否成功,如果不成功,就拋出出錯消息,終止客戶。 if (!printer) throw "Invalid proxy"; // 調用代理的printString 方法,把"Hello World!" 串傳給它。 // 服務器會在它的終端上打印這個串。 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) { try { ic->destroy(); } catch (const Ice::Exception& e) { cerr << e << endl; status = 1; } } return status; }RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成