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 ...
随机推荐
- Xshell利用lrzsz工具上传下载
直接安装这个lrzsz工具 yum install lrzsz 上传 rz 下载 sz
- SQLite使用笔记
前言 客户端存储信息的方法有好多种,在以往的项目中采用的是序列化记录到文件中的方式,即时通信项目中客户端的一些系统配置的保存也使用的这种方式,然而客户端保存聊天记录就不能使用这种方式(保存.读取.修改 ...
- 如何在hadoop上做等频离散化
抛砖引玉,先根据特征值group by,统计每个特征值出现次数,然后reduce到一个文件,根据一个文件来统计吧,毕竟,你知道多个桶,那么每个桶多少个样本就是确定了,数数,数到一个桶样本的时候停止,就 ...
- HDFS的一些重要流程
该随笔记录HDFS学习过程中遇到的比较重要的几个过程,包括:HDFS启动流程.DataNode备份流程.流程.写流程.删除流程.HDFS合并流程.这里都是从我的学习笔记中摘取出来的,如果哪里有误,还望 ...
- 在Linux服务器非root权限下搭建TensorFlow框架(Anaconda)
今天终于动手折腾实验室的服务器啦!由于权限原因,只能在自己的路径下安装TensorFlow. 1. 下载安装Anaconda 官网下载地址:https://www.anaconda.com/downl ...
- Gradient Boosting, Decision Trees and XGBoost with CUDA ——GPU加速5-6倍
xgboost的可以参考:https://xgboost.readthedocs.io/en/latest/gpu/index.html 整体看加速5-6倍的样子. Gradient Boosting ...
- day 07 数据类型,集合,深浅copy
1.day 06 内容回顾 小数据池 int :-5-256 str:特殊字符 ,*20 ascii:8位 1字节 表示一个字符 unicode:32位 4个字节 , 表示一个字符 字节表示8位表示一 ...
- dockerfile语法规则
编写Dockerfile 在前面的章节,我们学习了通过docker命令来下载镜像,运行镜像,在容器中部署自己的程序,最后将容器提交到自己的镜像中.但是,这并不是Docker推荐的镜像构建方式.在这一章 ...
- 转发:Webstorm 2017 破解激活下载
有用过一下,但是觉得比sublime重量太多,但是随着后来用node的开始,发现需要打造个web前端神器才能满足我的需求,于是乎重拾webstorm,目前发现11是新的版本,对node,npm支持性更 ...
- lr添加md5方法,字符编码转换,urlcode编码化
1.使得写lr脚本时可调用md5方法,需要进行以下操作: 1)将md5.h文件加载到Extra Files下: 2)在globals.h文件中添加 #include“md5.h” 3).打开md5文 ...