windows下thrift的使用(C++)

thrift cpp环境搭建:

1、  安装boost_1_53_0,注意,使用vs2010版本时,使用二进制的boost安装版本,生成的lib有可能是,在后续操作会出问题。在源码目录中,运行bootstrap.dat,然后会生成b2.exe文件,该文件可以编译boost需要的库,生成文件在boost\stage\lib文件下,后续对于lib的添加使用该路径。

2、  网络下载Win64OpenSSL_Light-1_1_0f.exe、Win64OpenSSL- 1_1_0f.exe,可以选择安装后者,前者是轻量级的不含源码的目标文件。

3、  下载thrift-0.9.0源码以及编辑器。打开thrift-0.9.0\thrift-0.9.0\lib\cpp中sln工程,编译libthrift工程和libthriftnb工程。前者为阻塞的,后者为非阻塞模式的服务器,非阻塞模式的服务器需要libevent库。

4、  对与编译libthrift工程,添加依赖头文件(C/C++ - general – Additional include directories):

C:\local\boost_1_53_0

C:\OpenSSL-Win64\include

添加依赖的链接库:(Linker – general – Additional library directories)

C:\local\boost_1_53_0\stage\lib

build该工程,会在debug文件夹中生成后续需要的libthrift.lib文件。

5、  对于写好的接口文件helloworld.thrift,通过命令行生成对应的接口文件,文件在gen-cpp中,其中含有HelloWorld_server.skeleton.cpp为服务器框架。

6、  通过vs2010创建空项目,添加gen-cpp中的头文件与资源文件,添加boost库的依赖头文件以及lib方法同上,添加thrift的相关文件为:

include项:C:\Users\Administrator\Desktop\thrift-0.9.0\thrift-0.9.0\lib\cpp\src

lib项:C:\Users\Administrator\Desktop\thrift-0.9.0\thrift-0.9.0\lib\cpp\Debug

此时编译可能出现“error LNK2019”,无法解析的外部符号,此问题由于缺少相应的库文件,解决方法为:Linker – input – Additional dependencies 添加libthrift.lib即可。

编译过程可能出现语句报错,譬如会有:using namespace ; 这种语句,注释掉即可。

7、  此时可以编译通过,但是会出错:

 

此时,在生成得到服务器框架的cpp中main函数中添加TWinsockSingleton::create();即可,在高版本的thrift已经不需要这一句了。

 

再次运行即可。

打开之前python的客户端,设置好端口,运行即可在服务器看到通信结果。

 

服务器的编写可以在生成的框架下进行修改,客户端的编写为:

#include "HelloWorld.h"

#include <thrift\transport\TSocket.h>
#include <thrift\transport\TBufferTransports.h>
#include <thrift\protocol\TBinaryProtocol.h>
#include <thrift\server\TSimpleServer.h>
#include <thrift\transport\TServerSocket.h>
//#include <thrift\transport\TBufferTransports.h>

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using boost::shared_ptr;
#include <iostream>
int main(int argc,char **argv)
{
	shared_ptr<TSocket> socket(new TSocket("127.0.0.1",9090));
	shared_ptr<TTransport> transport(new TBufferedTransport(socket));
	shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
	HelloWorldClient client(protocol);
	try
	{
		transport->open();
	}
	catch(TTransportException)
	{
		transport->close();
	}
	client.ping();
	std::string s;
	client.sayHello(s);
	std::cout<<s<<std::endl;
	getchar();
	return 0;
}

  

如上,其中sting的返回值需要修改服务器中sayHello对应函数,使得s获得一个返回值。

 

客户端工程同样需要添加以来库,同时将gen-cpp中的文件(除服务器框架文件)拷到目录下,添加到项目中。

 

Thrift 采用C/S模型,不支持双向通信,为了解决这个问题,通常在通信双方建立两个通信通道,开两个端口。

转载于:https://www.cnblogs.com/pinking/p/7739675.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值