zeroc ice 客户端与服务端通信例子(c++)
export ICE_HOME=/usr
1. Printer.ice
[root@linuxice ~]# cat Printer.ice
module Demo
{
interface Printer
{
void printString(string s);
};
};
2. slice2cpp Printer.ice
生成文件:Printer.cpp Printer.h
3. Server.cpp
[root@linuxice ~]# cat 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,Ice::stringToIdentity("SimplePrinter"));
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)
ic->destroy();
return status;
}
4. 编译服务端代码。在编译和连接时需要指定ICE头文件和动态链接库的位置
c++ -I. -I$ICE_HOME/include -c Printer.cpp Server.cpp
c++ -o server Printer.o Server.o -L$ICE_HOME/lib -lIce -lIceUtil
[root@linuxice ~]# c++ -I. -I$ICE_HOME/include -c Printer.cpp Server.cpp
[root@linuxice ~]#
[root@linuxice ~]#
[root@linuxice ~]#
[root@linuxice ~]# ls -tl |head
total 512
-rw-r--r-- 1 root root 62640 Jul 6 13:46 Server.o
-rw-r--r-- 1 root root 291472 Jul 6 13:46 Printer.o
-rw-r--r-- 1 root root 901 Jul 6 13:46 Server.cpp
-rw-r--r-- 1 root root 10235 Jul 6 11:55 Printer.cpp
[root@linuxice ~]# c++ -o server Printer.o Server.o -L$ICE_HOME/lib -lIce -lIceUtil
[root@linuxice ~]#
[root@linuxice ~]# ls -tl |head
total 704
-rwxr-xr-x 1 root root 193708 Jul 6 13:50 server
-rw-r--r-- 1 root root 62640 Jul 6 13:46 Server.o
-rw-r--r-- 1 root root 291472 Jul 6 13:46 Printer.o
-rw-r--r-- 1 root root 901 Jul 6 13:46 Server.cpp
-rw-r--r-- 1 root root 10235 Jul 6 11:55 Printer.cpp
5. Client.cpp文件
#include <Ice/Ice.h>
#include <Printer.h>
using namespace std;
using namespace Demo;
// 客户端只需要一个main函数,并且代码结构与服务端代码类似
int main(int argc, char* argv[])
{
int status = 0;
Ice::CommunicatorPtr ic;
try {
// 初始化ICE运行时
ic = Ice::initialize(argc, argv);
//ObjectPrx 在客户端代理服务器端
Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter:tcp -p 10000");
PrinterPrx printer =PrinterPrx::checkedCast(base);
if (!printer)
throw "Invalid proxy";
// 调用Printer对象代理的printString方法。调用将会通过对象代理被发送到服务端
printer->printString("HelloWorld!");
} 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$ICE_HOME/include -c Printer.cpp Client.cpp
c++ -o client Printer.o Client.o -L$ICE_HOME/lib -lIce -lIceUtil
[root@linuxice ~]# c++ -I. -I$ICE_HOME/include -c Printer.cpp Client.cpp
[root@linuxice ~]#
[root@linuxice ~]# ls -tl |head
total 776
-rw-r--r-- 1 root root 64600 Jul 6 14:12 Client.o
-rw-r--r-- 1 root root 291472 Jul 6 14:12 Printer.o
-rw-r--r-- 1 root root 827 Jul 6 14:10 Client.cpp
drwxr-xr-x 2 root root 4096 Jul 6 13:56 cl
-rwxr-xr-x 1 root root 193708 Jul 6 13:50 server
-rw-r--r-- 1 root root 62640 Jul 6 13:46 Server.o
-rw-r--r-- 1 root root 901 Jul 6 13:46 Server.cpp
-rw-r--r-- 1 root root 10235 Jul 6 11:55 Printer.cpp
-rw-r--r-- 1 root root 17540 Jul 6 11:55 Printer.h
c++ -o client Printer.o Client.o -L$ICE_HOME/lib -lIce -lIceUtil
[root@linuxice ~]# ls -tl |head
total 964
-rwxr-xr-x 1 root root 190836 Jul 6 14:14 client
-rw-r--r-- 1 root root 64600 Jul 6 14:12 Client.o
-rw-r--r-- 1 root root 291472 Jul 6 14:12 Printer.o
-rw-r--r-- 1 root root 827 Jul 6 14:10 Client.cpp
drwxr-xr-x 2 root root 4096 Jul 6 13:56 cl
-rwxr-xr-x 1 root root 193708 Jul 6 13:50 server
-rw-r--r-- 1 root root 62640 Jul 6 13:46 Server.o
-rw-r--r-- 1 root root 901 Jul 6 13:46 Server.cpp
-rw-r--r-- 1 root root 10235 Jul 6 11:55 Printer.cpp
6. 验证
crt窗口1执行:
[root@linuxice ~]# ./server
crt窗口2执行:
[root@linuxice ~]# ./client
crt窗口1显示:
[root@linuxice ~]# ./server
HelloWorld!
备注:之前的过程 的通信方式和细的东西再体会。