Ubuntu16.04下ZeroC ICE的安装与使用示例(Qt C++ 和 Java)
项目需求:在Ubuntu16.04系统下安装并使用ICEgrid 3.7进行c++和Java Springboot开发环境的通信,下面逐一介绍各个步骤的详解:
一:Ice Lib的安装
参考官网地址:https://doc.zeroc.com/ice/latest/release-notes/using-the-linux-binary-distributions#id-.UsingtheLinuxBinaryDistributionsv3.7-InstallingIceonUbuntu
首先添加Key和仓库地址:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv B6391CB2CFBA643D
sudo apt-add-repository -s "deb http://zeroc.com/download/Ice/3.7/ubuntu`lsb_release -rs` stable main"
更新软件包并安装:
sudo apt-get update
sudo apt-get install zeroc-ice-all-runtime zeroc-ice-all-dev
安装时可能会提示amd386的相关错误,忽视就行。如提示有缺失试试以下命令:
sudo apt-get upgrade -f
可以通过查看ice版本和slice版本的方法查看是否安装成功:
icegridNode -v
slice2cpp -v
二:Maven项目添加Ice依赖包
在pom.xml中添加如下依赖即可:
<repository>
<id>ice</id>
<name>ice Nexus Repository</name>
<url>https://repo.zeroc.com/nexus/content/repositories/releases/</url>
</repository> <dependency>
<groupId>com.zeroc</groupId>
<artifactId>ice</artifactId>
<version>3.7.2</version>
</dependency>
三:ice文件示例编写与切片
编写helloworld.ice:
module Demo {
interface Printer
{
void printString(string s);
};
};
分别用slice2cpp生成C++代码,用slice2java生成java代码
四:QT ice项目构建与编写
qmake.pro文件添加如下依赖:
LIBS += -L/usr/lib\
-lIce\
并将上一步生成的helloworld.cpp和helloworld.h放入项目中
用c++编写简单的服务端:
#include <QCoreApplication>
#include <Ice/Ice.h>
#include <helloworld.h> using namespace std;
using namespace Demo; // 从Printer抽象类派生
class PrinterI : public Printer {
public:
//ICE接口方法中都会自动增加一个参数 Ice::Current,不过在这个HelloWorld程序中我们不需要用到
virtual void printString(const string& s, const Ice::Current&){
cout << s << endl;
}
}; int main(int argc, char** argv)
{
int status = ; //退出状态
//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("HelloIce:default","tcp -p 10000"); // 实例化一个PrinterI对象,该对象将为接口Printer提供服务
Ice::ObjectPtr object = new PrinterI; // 把PrinterI对象加入ObjectAdapter,标识名为SimplePrinter。当有客户端请求Printer的服务时,ObjectAdapter将会把请求转给PrinterI对象
adapter->add(object, ic->stringToIdentity("HelloIce")); // 启动ObjectAdapter, 此后ObjectAdapter开始处理实际的调用请求
adapter->activate(); // 阻塞主线程,直到服务端的运行时被关闭
ic->waitForShutdown(); } catch (const Ice::Exception& e) {
cerr << e << endl;
status = ;
} catch (const char* msg) {
cerr << msg << endl;
status = ;
} // 程序结束时,需要销毁ICE运行时资源。如果在程序退出时没有对ICE运行时进行销毁,可能引起未知错误
if (ic)
{
try {
ic->destroy();
} catch (const Ice::Exception& e) {
cerr << e << endl;
status = ;
}
}
return status;
}
客户端:
#include <QCoreApplication>
#include <Ice/Ice.h>
#include <helloworld.h> using namespace std;
using namespace Demo; int main(int argc, char** argv)
{
Ice::CommunicatorPtr ic = Ice::initialize(argc, argv);
Ice::ObjectPrx proxy = ic->stringToProxy("HelloIce:default -p 10000");
PrinterPrx printer = PrinterPrx::checkedCast(proxy);
printer->printString("Hello Server");
ic->destroy();
cout << "End Client" << endl;
return ;
}
五:Java代码编写
接口实现类:
package com.demo.test.IceTest;
import com.zeroc.Ice.Current;
public class PrinterI implements Printer{
public void printString(String s, Current __current){
System.out.println(s);
}
}
服务器类:
package com.demo.test.IceTest; import com.zeroc.Ice.Communicator;
import com.zeroc.Ice.ObjectAdapter;
import com.zeroc.Ice.Object;
import com.zeroc.Ice.Util; public class IceTestServer { public static void main(String[] args){
try(Communicator communicator = Util.initialize(args)) {
ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints("HelloIce:default", "default -p 10000");
Object obj = new PrinterI();
adapter.add(obj, Util.stringToIdentity("HelloIce"));
adapter.activate();
communicator.waitForShutdown();
}
} }
客户端类:
//Ice接口测试,客户端
package com.demo.test.IceTest; import com.zeroc.Ice.Communicator;
import com.zeroc.Ice.ObjectPrx;
import com.zeroc.Ice.Util; public class IceTestClient { public static void main(String[] args){
System.out.println("Client Start");
Communicator ic = null;
ic = Util.initialize();
ObjectPrx proxy = ic.stringToProxy("HelloIce:default -p 10000");
PrinterPrx printer = PrinterPrx.checkedCast(proxy);
printer.printString("Hello server");
ic.destroy();
System.out.println("Client Destroy");
}
}
常见问题:
1.本文所用ice版本是3.7.2,所生成的java文件有3个:_PrinterPrxl.java, Printer.java, PrinterPrx.java,若ice版本不同生成文件可能也不同,编程规范也不同
2.在Linux系统下若使用小于1024的端口号会提示权限错误,出现该错误改端口号即可。
Ubuntu16.04下ZeroC ICE的安装与使用示例(Qt C++ 和 Java)的更多相关文章
- Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 在这篇博客里,我采用了非官网的安装步骤,来进行安装.走了弯路,同时,也是不建议.因为在大数据领域和实际生产里,还是要走正规的为好. Ubuntu16.04下Mongodb(离线安 ...
- Ubuntu16.04下LAMP环境的安装与配置
Ubuntu16.04下LAMP环境的安装与配置 最近做个实验需要用到Ubuntu环境的靶场,所以这里介绍下Ubuntu环境下LAMP的安装与配置,话不多说,我们gkd! 1.Apache2的安装 首 ...
- Ubuntu16.04下Mongodb(离线安装方式|非apt-get)安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 说在前面的话 首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu LTS \n \l r ...
- ubuntu16.04下笔记本电脑扩展双屏安装过程
想给笔记本电脑外界一个显示屏,因为科研需要,我的笔记本是windows10加Ubuntu16.04双系统,主要使用Ubuntu系统. 首先是硬件 一个外置显示屏是必须的了,然后我的笔电上只有HDMI接 ...
- ubuntu16.04下ftp服务器的安装与配置
由于要将本地程序上传至云服务器中,所以需要给云服务器端安装ftp服务器.记录一下ftp的安装过程,以便以后使用.服务器端所用系统为Ubuntu16.04. 1. 安装ftp服务器, apt-get i ...
- Ubuntu16.04下Python2:pip安装opendr库
在Ubuntu16.04/Python2环境安装opendr遇到了问题,并且报错不清楚. 使用dis_to_free的方法很好地解决问题. sudo apt install libosmesa6-de ...
- Ubuntu16.04下Hadoop的本地安装与配置
一.系统环境 os : Ubuntu 16.04 LTS 64bit jdk : 1.8.0_161 hadoop : 2.6.4 部署时使用的用户名为hadoop,下文中需要使用用户名的地方请更改为 ...
- [转]Ubuntu16.04下ralink rt3290驱动安装
出处:https://askubuntu.com/questions/253632/how-do-i-get-a-ralink-rt3290-wireless-card-working 解决为问题:L ...
- Ubuntu16.04 下 python 3.6 安装以及各版本python切换(同时解决各种依赖缺失)
有些博客给出了从源代码通过./configure.make.sudo make 会导致安装玩之后出现各种依赖缺失的问题,如_sqlite3._bz2 等问题. 当然也有很多帖子给出了从系统自带的pyt ...
随机推荐
- 第八届蓝桥杯 4、方格分割 DFS
标题:方格分割 6x6的方格,沿着格子的边线剪开成两部分. 要求这两部分的形状完全相同. 如图:p1.png, p2.png, p3.png 就是可行的分割法. 试计算: 包括这3种分法在内,一共有多 ...
- 解决安卓JNI native 线程不能正常退出问题二
直面这个解决方法的可以看我转载的博客 https://www.cnblogs.com/Carlsblog/p/9438016.html 本方法是个投机取巧法,不过也解决了不能正常 ...
- 搭建k8s(一)
安装VMWare VMWare官网地址 点击下载-->WorkStation Pro-->点击linux免费试用版 下载安装完成后,创建一个虚拟机,去centos官网找到centos7is ...
- Node.js 8 中的 util.promisify的详解
Node.js 8带来了 很多新特性 .其中比较值得注意的,便有 util.promisify() 这个方法. util.promisify() 虽然 Promise 已经普及,但是 Node.js ...
- div模拟文本框textarea
需求:利用highlight.js对文本框中的内容进行高亮显示 1.highlight.js使用 js中:<script src="js/highlight/highlight.pac ...
- ES5 & ES6 基础
一.什么是ES5 附上一览表 (5.1中文 (2011.6)): http://lzw.me/pages/ecmascript/ (5.1英文PDF):http://www.ecma-internat ...
- EXCE 表格导入导出遇到问题(easypoi)
使用Easypoi进行excel表格的导入导出遇到的问题: 1.导出时候打开文件一直遇乱码,主要的原因就是我在实体类没有进行给每个字段进行注解,就会导致每个字段找不到对应的汉字表头,所以一定不要忘了导 ...
- Shell脚本【扔一百次硬币】
#!/bin/bash#扔一百次硬币,然后分别显示出正面和反面的次数! for i in $(seq 100) do if [ `echo $((RANDOM%2))` == 0 ] then let ...
- js检测页面离开
window.location = 'yjk://app.h5.ihaozhuo.com?page=livetrailer&videoLiveId=' + parseInt(this.Requ ...
- explode() 字符串分割函数
说明 本函数返回由字符串组成的数组,其中的每个元素都是由 separator 作为边界点分割出来的子字符串. separator 参数不能是空字符串.如果 separator 为空字符串(" ...