ICE网络通信的例子

1.环境

采用ice3.7(ICE3.7库文件和头文件下载)和VS2015_32_64_Debug_Release进行开发,配置好项目的包含目录和链接目录
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.编写.ice文件

新建文件Printer.ice

module Demo {
interface Printer {
void printString(string s);
int add(int num1,int num2);
int sub(int num1,int num2);
};
};

3.编译Print.ice文件

找到对应版本下面的slice2cpp.exe
在这里插入图片描述
使用命令行编译Printer.ice文件

slice2cpp.exe F:\VS2015\VS2015Code\Test\IceServer\Printer.ice

在这里插入图片描述
然后和slice2cpp.exe同一个目录下面找到编译出来的两个文件Printer.h和Printer.cpp
在这里插入图片描述
把这两个文件拷贝到Server项目目录下面

4.新建子类继承自Printer

在这里插入图片描述

#pragma once

#include <Ice/Ice.h>
#include <string>
#include "Printer.h"

class MyPrinter :public Demo::Printer {
public:
	//ICE接口方法中都会自动增加一个参数 Ice::Current,不过在这个HelloWorld程序中我们不需要用到
	virtual void printString(const std::string&str, const Ice::Current&);
	virtual int add(int num1, int num2, const Ice::Current&);
	virtual int sub(int num1, int num2, const Ice::Current&);
};

#include "MyPrinter.h"
#include <stdio.h>
void MyPrinter::printString(const std::string&str, const Ice::Current&) {
	std::string outStr = str;
	
	std::cout << str << std::endl;

	int a = 0;
	a++;
}

int MyPrinter::add(int num1, int num2, const Ice::Current&) {
	std::cout << num1 + num2 << std::endl;
	return num1 + num2;
}

int MyPrinter::sub(int num1, int num2, const Ice::Current&) {
	std::cout << num1 - num2 << std::endl;
	return num1 - num2;
}

5.创建服务通信

#include "Ice/Ice.h"
#include "MyPrinter.h"

int main(int argc, char *argv[]) {
	int status = 0;  //退出状态
	//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("SimplePrinterAdapter", "default -p 10002");

		// 实例化一个PrinterI对象,该对象将为接口Printer提供服务
		Ice::ObjectPtr object = new MyPrinter();

		// 把PrinterI对象加入ObjectAdapter,标识名为SimplePrinter。当有客户端请求Printer的服务时,ObjectAdapter将会把请求转给PrinterI对象
		adapter->add(object, ic->stringToIdentity("myprinter"));

		// 启动ObjectAdapter, 此后ObjectAdapter开始处理实际的调用请求
		adapter->activate();

		std::cout << "==>  server started" << std::endl;

		// 阻塞主线程,直到服务端的运行时被关闭
		ic->waitForShutdown();

	} catch (const Ice::Exception& e) {
		status = 1;
	} catch (const char* msg) {
		status = 1;
	}

	// 程序结束时,需要销毁ICE运行时资源。如果在程序退出时没有对ICE运行时进行销毁,可能引起未知错误
	if (ic) {
		try {
			ic->destroy();
		} catch (const Ice::Exception& e) {
			status = 1;
		}

	}
	return 0;
}

编译即可生成对应的服务端

6.建立客户端

把Printer.h和Printer.cpp拷贝到客户端项目中,
在这里插入图片描述

7.建立客户端连接

#include "Ice/Ice.h"
#include "Printer.h"

int main(int argc, char *argv[]) {
	int status = 0;
	Ice::CommunicatorPtr ic;
	try {
		// 初始化ICE运行时
		ic = Ice::initialize(argc, argv);

		// 使用字符串来生成一个对象代理
		// 对象代理包含有服务端的服务对象的方法定义,并且负责和服务端的对象进行通信。因此客户端可以像使用本地对象一样使用服务端的对象
		// 字符串中指定了对象代理对应的服务端的对象的名称,以及服务端对象监听的协议和端口。这些信息需要跟服务端中定义的信息一致
		//ObjectPrx 在客户端代理服务器端
#if 0
		Ice::ObjectPrx base = ic->stringToProxy("myprinter:default -h 127.0.0.1 -p 10002");
#else
		Ice::ObjectPrx base = ic->stringToProxy("myprinter:default -p 10002");
#endif
		// 上面得到的是一个范型的对象代理,需要将它转换为Printer对象的代理,这样才能调用printString方法。
		// checkedCast会与服务端进行通信,以判断该对象代理能否成功转换为Printer对象代理。如果转换失败,将返回空对象代理
		Demo::PrinterPrx printer = Demo::PrinterPrx::checkedCast(base);
		if (!printer) {
			throw "Invalid proxy";
		}
		// 调用Printer对象代理的printString方法。调用将会通过对象代理被发送到服务端
		printer->printString("你好,服务器");

		printer->add(10, 36);
		printer->sub(100, 36);
	} catch (const Ice::Exception&ex) {
		status = 1;
	} catch (const char* msg) {
		status = 1;
	}
	if (ic)
		ic->destroy();
	return status;
}

配置好相应的包含目录和链接目录,编译即可生成客户端

8.运行

首先运行服务端,没点击一次客户端,就会和服务端进行一次通信
在这里插入图片描述

aaa

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值