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 ...
随机推荐
- git 拉取远程代码
git 拉取远程代码 || 利用vscode编辑器自带了git,可在ctrl+~打开控制台拉取代码,非常好用哦~在实际项目开发过程中,往往是已经存在远程项目了,我们定义的需求是只需要简单的操作git, ...
- linux添加本地yum源
增加一块硬盘 然后格式化成btrfs格式的文件 mkfs.btrfs /dev/sde 永久挂载 echo "/dev/sde /mnt btrfs defaults 0 0" & ...
- java 删除整数元素集合中的元素
1. 简介 对于整数类型的元素集合,例如{1, 2, 3, 4, 5},再进行元素删除时需要注意.在List中删除操作有remove(int index)和remove(Object o), 查看两种 ...
- 洛谷P3627[APOI2009] (讨厌的)抢掠计划
题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...
- php遍历数组7种方式(严格说是五种)
数组: $arr = array(1,2,3,4,5); 第一种:foreach (最常见的) foreach ($arr as $v){ echo $v;} 第二种:for for($i=0;$i& ...
- ClientDataSet使用locate或Filter定位到字段为空值的记录
场景,程序想检查是否存在某个字段的值是空的,如果存在,则不允许增加记录,否则允许增加记录. 解决这个问题,我一开始用了两种错误的方法 if not clientdataset.locate('AFie ...
- MySQL union all排序问题
mysql中多个查询结果排序后union之后是混乱的,可以在各个查询结果最后加limit 999999999再union,参考https://blog.csdn.net/chenyao1994/art ...
- 大数据之NBA
2017年NBA季后赛落幕后,网络上仍有大量关于NBA关键词的搜索. 这是近30天来,通过PC端和移动端搜索的趋势,可以看到有些日期的搜索指数是猛增的,因为NBA有大新闻爆出,比如8月23日,骑士与凯 ...
- JDBCUtils——C3P0
需要导入的包: mysql-connector-java-5.1.37-bin.jar c3p0-0.9.2-pre5.jar mchange-commons-java-0.2.3.jar 如果使用D ...
- [Oracle][RMAN] Use RMAN to Migrate database from CentOS_5-11201-SingleDB to OracleLinux_5-11204-SingleDB
リンク:How to Move/Restore DB to New Host and File System using RMAN (Doc ID 1338193.1)https://docs.ora ...