windows下thrift的使用(C++)

windows下thrift的使用(C++)

thrift cpp环境搭建:

1、  安装boost_1_53_0,注意,使用vs2010版本时,使用二进制的boost安装版本,生成的lib有可能是<span>windows下thrift的使用(C++)</span>,在后续操作会出问题。在源码目录中,运行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、  此时可以编译通过,但是会出错:

 <span>windows下thrift的使用(C++)</span>

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

 <span>windows下thrift的使用(C++)</span>

再次运行即可。

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

 <span>windows下thrift的使用(C++)</span>

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

#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模型,不支持双向通信,为了解决这个问题,通常在通信双方建立两个通信通道,开两个端口。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/119490.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号