使用Ice::Application可以简化一部分代码:
服务端:
#pragma once
#include
class IceClientApp:public Ice::Application {
public:
IceClientApp();
~IceClientApp();
virtual int run(int argc, char* argv[])override;
};
#include "IceClientApp.h"
#include
#include
#include "Printer.h"
static int count = 0;
IceClientApp::IceClientApp() {
}
IceClientApp::~IceClientApp() {
}
int IceClientApp::run(int argc, char* argv[]) {
Ice::CommunicatorPtr ic = communicator();
Ice::ObjectPrx base = ic->stringToProxy("TestPrinter:tcp -h localhost -p 10002");
Demo::PrinterPrx printer = Demo::PrinterPrx::checkedCast(base);
if (!printer) {
throw "Invalid Proxy!";
}
while (true){
std::string strCount = QString::number(++count).toStdString();
printer->printString(strCount + " Hello World!");
std::cout << printer->add(count, count) << std::endl;
Sleep(1000);
}
return 0;
}
ice文件
module Demo {
interface Printer {
void printString(string s);
int add(int num1,int num2);
int sub(int num1,int num2);
};
};
#include "IceClientApp.h"
int main(int argc, char *argv[]) {
IceClientApp app;
return app.main(argc,argv);
}
客户端:
#pragma once
#include
class IceApp:public Ice::Application {
public:
IceApp();
~IceApp();
virtual int run(int argc, char* argv[])override;
};
#include "IceApp.h"
#include "TestPrinter.h"
#include
IceApp::IceApp() {
}
IceApp::~IceApp() {
}
int IceApp::run(int argc, char* argv[]) {
Ice::CommunicatorPtr&ic = communicator();
Ice::ObjectAdapterPtr objAdapter = ic->createObjectAdapterWithEndpoints("TestAdapter", "tcp -h localhost -p 10002");
Ice::ObjectPtr obj =new TestPrinter();
objAdapter->add(obj, ic->stringToIdentity("TestPrinter"));
objAdapter->activate();
ic->waitForShutdown();
return 0;
}
TestPrinter
#pragma once
#include "Printer.h"
class TestPrinter : public Demo::Printer {
public:
TestPrinter();
~TestPrinter();
virtual void printString(const ::std::string& s, const ::Ice::Current& current = ::Ice::emptyCurrent) override;
virtual ::Ice::Int add(::Ice::Int num1, ::Ice::Int num2, const ::Ice::Current& current = ::Ice::emptyCurrent) override;
virtual ::Ice::Int sub(::Ice::Int num1, ::Ice::Int num2, const ::Ice::Current& current = ::Ice::emptyCurrent)override;
};
#include "TestPrinter.h"
TestPrinter::TestPrinter() {
}
TestPrinter::~TestPrinter() {
}
void TestPrinter::printString(const ::std::string& s, const ::Ice::Current& current /*= ::Ice::emptyCurrent*/) {
std::cout << s << std::endl;
}
::Ice::Int TestPrinter::add(::Ice::Int num1, ::Ice::Int num2, const ::Ice::Current& current /*= ::Ice::emptyCurrent*/) {
return num1 + num2;
}
::Ice::Int TestPrinter::sub(::Ice::Int num1, ::Ice::Int num2, const ::Ice::Current& current /*= ::Ice::emptyCurrent*/) {
return num1 - num2;
}
main
#include "IceApp.h"
int main(int argc, char *argv[]) {
IceApp app;
return app.main(argc, argv);
}