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 ...
随机推荐
- 找出点的密集区域,javascript实现,html5 canvas效果图
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- zzw_非root用户安装python3.5
目的:不想改变linux 自带的python,只想要用特定的非root用户运行特定版本的python 1.进入非root用户 2.新建一个python3.5的安装目录 [a4_csbdc@bdc816 ...
- css 积累
1.input 初始化 input { -webkit-tap-highlight-color: rgba(0,0,0,0); border: none; } input:focus { outlin ...
- Jenkins自动构建gitlab项目(jenkins+maven+giltlab+tomcat)
环境准备: System:CentOS 7.3 (最小化安装) JDK: 8u161 (1.8_161) tomcat: 8.5.29 Jenkins: Jenkins 2.107.1 Gitlab: ...
- vbs 去掉字符串中的空格
今天在写自动化脚本时,需要把字符串中的空格替换成其他特殊字符,但字符间的空格个数又不确定,经过搜索,成功解决. 解决重点就是把每个空格字符串搜索出来,然后进行替换,主要用到space函数. strSt ...
- JVM工具jstat使用说明
输入:jstat -help得到以下帮助信息 Usage: jstat --help|-options jstat -<option> [-t] [-h<lines>] < ...
- 版本管理工具Git(2)git的使用
上一篇带大家认识了Git,在本篇中将讲解Git的安装及使用: Git系列导航 版本管理工具Git(1)带你认识git 版本管理工具Git(2)git的安装及使用 版本管理工具Git(3)VS下如何使用 ...
- git提示错误关于错误:ssh: Could not resolve hostname github.com: Name or service not known.fatal: Could not read from remote repository.
关于 Git 使用中出现的错误 饥人谷_楠柒 关注 2016.11.02 15:33* 字数 746 阅读 3607评论 5喜欢 10赞赏 1 关于错误:ssh: Could not resolve ...
- stackExchange.redis 实现模糊匹配批量查询
如果使用redis的频次较高,那么业务中经常会出现需要根据关键字进行批量查询,所以总结一下StackExchange中使用批量查询的方法(如果数据量很大,那么在redis中模糊查询很耗时,请慎用!) ...
- winfrom程序Datagridview列名问题
之前在做程序的时候,有遇到过这个问题: 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColume"解决方法 解决办法 ...