采用ice3.7(ICE3.7库文件和头文件下载)和VS2015_32_64_Debug_Release进行开发,配置好项目的包含目录和链接目录
新建文件Printer.ice
module Demo {
interface Printer {
void printString(string s);
int add(int num1,int num2);
int sub(int num1,int num2);
};
};
找到对应版本下面的slice2cpp.exe
使用命令行编译Printer.ice文件
slice2cpp.exe F:\VS2015\VS2015Code\Test\IceServer\Printer.ice
然后和slice2cpp.exe同一个目录下面找到编译出来的两个文件Printer.h和Printer.cpp
把这两个文件拷贝到Server项目目录下面
#pragma once
#include
#include
#include "Printer.h"
class MyPrinter :public Demo::Printer {
public:
//ICE接口方法中都会自动增加一个参数 Ice::Current,不过在这个HelloWorld程序中我们不需要用到
virtual void printString(const std::string&str, const Ice::Current&);
virtual int add(int num1, int num2, const Ice::Current&);
virtual int sub(int num1, int num2, const Ice::Current&);
};
#include "MyPrinter.h"
#include
void MyPrinter::printString(const std::string&str, const Ice::Current&) {
std::string outStr = str;
std::cout << str << std::endl;
int a = 0;
a++;
}
int MyPrinter::add(int num1, int num2, const Ice::Current&) {
std::cout << num1 + num2 << std::endl;
return num1 + num2;
}
int MyPrinter::sub(int num1, int num2, const Ice::Current&) {
std::cout << num1 - num2 << std::endl;
return num1 - num2;
}
#include "Ice/Ice.h"
#include "MyPrinter.h"
int main(int argc, char *argv[]) {
int status = 0; //退出状态
//ic是一个指向ICE运行时资源的智能指针,通过ic可以获取运行时的各种资源。
Ice::CommunicatorPtr ic;
try {
// Ice::initialize 初始化一个ICE运行时。传入 argc,argv是因为服务代码可能会处理命令行参数(本例中不需要)。
ic = Ice::initialize(argc, argv);
// 创建一个 ObjectAdapterPtr adapter,名字为 SimplePrinterAdapter。这个Adapter监听TCP/IP的10000端口
Ice::ObjectAdapterPtr adapter =
ic->createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10002");
// 实例化一个PrinterI对象,该对象将为接口Printer提供服务
Ice::ObjectPtr object = new MyPrinter();
// 把PrinterI对象加入ObjectAdapter,标识名为SimplePrinter。当有客户端请求Printer的服务时,ObjectAdapter将会把请求转给PrinterI对象
adapter->add(object, ic->stringToIdentity("myprinter"));
// 启动ObjectAdapter, 此后ObjectAdapter开始处理实际的调用请求
adapter->activate();
std::cout << "==> server started" << std::endl;
// 阻塞主线程,直到服务端的运行时被关闭
ic->waitForShutdown();
} catch (const Ice::Exception& e) {
status = 1;
} catch (const char* msg) {
status = 1;
}
// 程序结束时,需要销毁ICE运行时资源。如果在程序退出时没有对ICE运行时进行销毁,可能引起未知错误
if (ic) {
try {
ic->destroy();
} catch (const Ice::Exception& e) {
status = 1;
}
}
return 0;
}
编译即可生成对应的服务端
把Printer.h和Printer.cpp拷贝到客户端项目中,
#include "Ice/Ice.h"
#include "Printer.h"
int main(int argc, char *argv[]) {
int status = 0;
Ice::CommunicatorPtr ic;
try {
// 初始化ICE运行时
ic = Ice::initialize(argc, argv);
// 使用字符串来生成一个对象代理
// 对象代理包含有服务端的服务对象的方法定义,并且负责和服务端的对象进行通信。因此客户端可以像使用本地对象一样使用服务端的对象
// 字符串中指定了对象代理对应的服务端的对象的名称,以及服务端对象监听的协议和端口。这些信息需要跟服务端中定义的信息一致
//ObjectPrx 在客户端代理服务器端
#if 0
Ice::ObjectPrx base = ic->stringToProxy("myprinter:default -h 127.0.0.1 -p 10002");
#else
Ice::ObjectPrx base = ic->stringToProxy("myprinter:default -p 10002");
#endif
// 上面得到的是一个范型的对象代理,需要将它转换为Printer对象的代理,这样才能调用printString方法。
// checkedCast会与服务端进行通信,以判断该对象代理能否成功转换为Printer对象代理。如果转换失败,将返回空对象代理
Demo::PrinterPrx printer = Demo::PrinterPrx::checkedCast(base);
if (!printer) {
throw "Invalid proxy";
}
// 调用Printer对象代理的printString方法。调用将会通过对象代理被发送到服务端
printer->printString("你好,服务器");
printer->add(10, 36);
printer->sub(100, 36);
} catch (const Ice::Exception&ex) {
status = 1;
} catch (const char* msg) {
status = 1;
}
if (ic)
ic->destroy();
return status;
}
配置好相应的包含目录和链接目录,编译即可生成客户端
aaa