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 ...
随机推荐
- multiThread (一)
并发系列(1)之 Thread 详解 阅读目录 一.线程概述 二.线程状态 三.源码分析 1. native注册 2. 构造方法和成员变量 3. start 方法 4. exit 方法 5. 弃用 ...
- 在vue中调用echarts中的地图散点图~
首先!当然是在vue中引入echarts! 命令行 npm install echarts --save 在main.js文件中里引入 import echarts from 'ech ...
- ASP.NET上传大文件报错,IIS7.0
打开你系统盘(我是C盘),找到C:\Windows\System32\inetsrv\config\schema目录,该目录下有一个IIS_schema.xml,右击打开文件,Ctrl+F,然后输入& ...
- K8s+jenkins实现提升效率 —— 一些小记录
尝试下K8s + jenkins的组合,非常方便.在这里记录一下: kubernetes版本: 1.10 + deployment.yaml apiVersion: v1 kind: Service ...
- C# 今天时间 今天结束时间
var startTime = DateTime.UtcNow.ToString(timeFormat); ).AddSeconds(-).ToUniversalTime().ToString(tim ...
- 苹果手机的SB系列(3)超级烦人的账户解锁?
不知道大家有没有这种体验,Iphone 每隔一段时间后账户就被锁定了,也不告诉你原因,就是要解锁? 我怎么感觉比做的比支付宝差多了,我注册支付宝十几年,也没有动不动就告诉了有安全原因,要解锁,要重置密 ...
- HIbernate常见异常(转载)
SSH阶段常见的异常系列之一hibernate(15条) 异常一 异常一 异常描述: Sax解析异常:cvc-复杂的类型,发现了以元素maping开头的无效内容,应该是以 ‘{“http://www. ...
- 使用docker安装sentry
一.安装docker yum -y install docker 更换docker镜像源为中科大的 在配置文件/etc/docker/daemon.json中加入 { "registry-m ...
- Java作业:第一次过程性考核 ——长春职业技术学院 16级网络工程
Java作业:第一次过程性考核 ••<结构化程序设计>•• 考核目标:初步了解Java基本语法规则,学习结构化程序设计思想. 码云链接:https://gitee.com/SoridoD/ ...
- 浅谈java 之 Map
先来一张Map的类继承图 Map :Hashtable .HashMap .LinkedHashMap .TreeMap 的比较 1.Hashtable的方法实现了synchronized 是线程 ...