一、c++实例

1.下载与安装thrift工具

http://thrift.apache.org/download/

、服务端代码

1)、新建vc工程。

2)、将上面的文件拷贝到工程目录下,Test_server.skeleton.cpp,就是C++服务端的main函数入口文件。

// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.

#include "Test.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 namespace ::apache::thrift::server;

using boost::shared_ptr;

class TestHandler : virtual public TestIf {
 public:
  TestHandler() {
    // Your initialization goes here
	  Init();
  }

  int32_t add(const int32_t a, const int32_t b) {
    // Your implementation goes here
    printf("add\n");
	return a+b;
  }

  void getById(User& _return, const int64_t id) {
    // Your implementation goes here
	User *user;
	std::list<User *>::iterator iter = m_listUser.begin(),iterEnd = m_listUser.end();
	for(iter; iter!=iterEnd; iter++)
	{
		user = *iter;
		if(user->id == id)
		{
			_return.id = user->id;
			_return.__set_name(user->name.c_str());
			_return.age = user->age;
			_return.__set_vip(user->vip);
			break;
		}
	}
	printf("getById\n");
  }

protected:
	void Init(){
		m_listUser.clear();
		int n = 0;
		for(n=0; n<10; n++){
			User * user = new User();
			user->id = n+1;
			user->age = 18+n;
			sprintf((char *)user->name.data(),"name_%d",n+1);
			//插入
			m_listUser.push_back(user);
		}

	}
	std::list<User *> m_listUser;
};

int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<TestHandler> handler(new TestHandler());
  shared_ptr<TProcessor> processor(new TestProcessor(handler));
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  server.serve();
  return 0;
}

//需要引入thrift对于cpp的lib头文件所在目录,openssl头文件所在目录,boost头文件所在目录。

//需要引用lib文件,LibThrift.lib libeay32MT.libssleay32MT.lib

5、客户端代码

1)、新建vc工程。

2)、将上面的文件(除Test_server.skeleton.cpp外)拷贝到工程目录下。

3)、编写客户端代码。

// Client.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include "Test.h"

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

using boost::shared_ptr;
using namespace std;

int main(int argc, char* argv[])
{
	boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
	//对接nonblockingServer时必须的,对普通server端时用boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
	//boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
	boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));

	boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
	TestClient client(protocol);

	try
	{
		//设置发送、接收、连接超时
		socket->setConnTimeout(5000);
		socket->setRecvTimeout(5000);
		socket->setSendTimeout(5000);

		transport->open();

		//insert your code here
		int nResult = client.add(100,200);
		printf("add(%d,%d) = %d\n",100,200,nResult);
		User findUser;
		findUser.id = -100;
		client.getById(findUser,1);
		printf("%s\n",findUser.name.c_str());

		transport->close();

		printf("send success!\n");
	}
	catch (...)
	{
	}

	return 0;
}

//需要引入thrift对于cpp的lib头文件所在目录,openssl头文件所在目录。

//需要引用lib文件,LibThrift.lib libeay32MT.libssleay32MT.lib

二、VS2008上编译thrift的库文件

1、下载thrift-0.9.3.tar.gz源码,源码针对cpp的编译自带的是vs2010版本。

2、需要第三方库boost、libevent、openssl支持。

libevent-2.0.21-stable.tar.gzhttps://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

boost 1.54.0

http://www.boost.org/users/history/version_1_54_0.html

编译好的boost 1.54.0下载地址:https://sourceforge.net/projects/boost/files/boost-binaries/1.54.0

OpenSSL 1.0.0g

VC2008下使用OpenSSL 1.0.0g(免编译)

http://blog.csdn.net/akof1314/article/details/7241829

3、新建vc工程,编译。

用Project From Existing Code...创建工程。
将 cpp/src/thrift加入工程,修改项目配置类型修改成静态库。
在Solution Explorer中,
去除 qt目录。
添加 boost包含目录、openssl所包含目录。
添加 src为包含目录,不然 #include <thrift/Thrift.h>出错。
VC2008缺少 stdint.h,创建到thrift/windows目录下,并添加为include目录。
#pragma once

#include <boost/cstdint.hpp>

typedef boost::int8_t int8_t;

typedef boost::uint8_t uint8_t;

typedef boost::int16_t int16_t;

typedef boost::uint16_t uint16_t;

typedef boost::int32_t int32_t;

typedef boost::uint32_t uint32_t;

typedef boost::int64_t int64_t;

typedef boost::uint64_t uint64_t;

#define INT8_MIN    ((int8_t)_I8_MIN)

#define INT8_MAX    _I8_MAX

#define INT16_MIN   ((int16_t)_I16_MIN)

#define INT16_MAX   _I16_MAX

#define INT32_MIN   ((int32_t)_I32_MIN)

#define INT32_MAX   _I32_MAX

#define INT64_MIN   ((int64_t)_I64_MIN)

#define INT64_MAX   _I64_MAX

#define UINT8_MAX   _UI8_MAX

#define UINT16_MAX  _UI16_MAX

#define UINT32_MAX  _UI32_MAX

#define UINT64_MAX  _UI64_MAX

去除VC2010 thrift.sln中没有的文件,无法编译:
Mutex.cpp, PosixThreadFactory.cpp, Monitor.cpp
TEvhttpClientChannel.cpp TEvhttpServer.cpp
TServer.cpp TNonblockingServer.cpp
TSSLSocket.cpp TSSLServerSocket.cpp
TZlibTransport.cpp

加上宏 HAVE_CONFIG_H,或者 force include force_inc.h.
windows/tr1/functional中
#include <functional>
改为
#include <boost/tr1/functional.hpp>


Thrift之c++实例的更多相关文章

  1. Thrift-java实例

    ➠更多技术干货请戳:听云博客 Thrift实例1 功能描述:客户端与服务器端分别是两个应用,先启动服务器端,再启动客户端,实现执行客户端运行服务器端的加法方法. 源码截图(源码在附件中): 客户端: ...

  2. Thrift之java实例

    一.java实例 1.下载与安装thrift工具 http://thrift.apache.org/download/ .服务器代码 服务Test实现类 package com.zychen.thri ...

  3. Thrift全面介绍

    官网:http://thrift.apache.org   简介 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java ...

  4. thrift常见异常及原因分析(updating)

    [org.apache.thrift.TException家族] [Thrift架构] 以下是thrift的客户端和服务端交互的一个原理图.可以看到遵循了rpc框架的传输层.协议层和应用层三层.本文提 ...

  5. Thrift初试

    Restful 基于 Http 进行通讯. 开放.标准.简单.兼容性升级容易: 性能略低.在 QPS 高或者对响应时间要求苛刻的服务上,可以用 RPC,RPC采用二进制传输.TCP 通讯,所以通常性能 ...

  6. python thrift 实现 单端口多服务的过程

    Thrift 是一种接口描述语言和二进制通信协议.以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过 Thrift 进行通信的,然后写自动化脚本之前研究了一下. 需要定义一个 ...

  7. 北风风hadoop课程体系

    课程一.基于Linux操作系统平台下的Java语言开发(20课时)课程简介本套课程主要介绍了Linux系统下的Java环境搭建及最基础的Java语法知识.学习Linux操作系统下Java语言开发的好处 ...

  8. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  9. Hive 官方手册翻译 -- Hive Transactions (Hive 事务)

    由 Alan Gates创建, 最终由 Andrew Sherman修改于2018年8月7日 原文链接:https://cwiki.apache.org/confluence/display/Hive ...

随机推荐

  1. 【算法题12 解码方法decode way】

    1.来源LeetCode91 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请 ...

  2. 关于C# yield 你会使用吗?

    假设有这样一个需求:在一个数据源(下面代码arry)中把其中大于4的数据取出来遍历到前台,怎么做?(不使用linq) , , , , , , , , , }; 第一种情况:  不使用yield的情况下 ...

  3. c# 图片加密解密的实例代码

    c# 图片加密解密的实例代码. 代码: using System; using System.Collections.Generic; using System.Text; using System. ...

  4. MAC 终端颜色设置

    在bash中,可以通过更改PS1环境变量的值来设置提示行.通常的提示符颜色单调,用户可以通过在PS1中添加颜色代码序列来设置提示符中不同信息以不同颜色显示. 添加颜色相当容易:第一步是设计不带颜色的提 ...

  5. Oracle事务和锁机制

    事务 1. 说明 一组SQL,一个逻辑工作单位,执行时整体修改或者整体回退. 2.事务相关概念 1)事务的提交和回滚:COMMIT/ROLLBACK 2)事务的开始和结束 开始事务:连接到数据库,执行 ...

  6. MapReduce概述

    MapReduce 源自于Google的MapReduce论文,Hadoop MapReduce是Google MapReduce克隆版 MapReduce适合PB级以上海量数据的离线处理 MapRe ...

  7. E-R图和数据库的设计

    数据库设计: 原则:如果属性有了多个字段,可以当实体.如果只有一个字段,只能当属性(比如实体属性种类) 1.设计E-R图 实体:矩形 关系:菱形 属性:椭圆(可省) 2.关系的类型 一对一 一对多 多 ...

  8. node操作mongodb

    var mongodb = require('mongodb'); var server = new mongodb.Server('localhost', 27017, {auto_reconnec ...

  9. Linux系统crontab定时调度Python脚本

    Linux系统crontab定时调度Python脚本 一.Python脚本随Linux开机自动运行 #Python脚本:/home/edgar/auto.py #用root权限编辑以下文件:/etc/ ...

  10. Eclipse使用Maven搭建Java Web项目并直接部署Tomcat

    1.环境: Windows 10 Java 1.8 Maven 3.3.9 Eclipse IDE for Java EE Developers 2.准备: eclipse环境什么的不赘述,Maven ...