Thrift C++ 服务器和客户端开发实例--学习笔记二

本文是关于Thrift C++的开发学习笔记,涵盖了如何创建服务器和客户端。通过Thrift命令生成C++代码,并提供了server.cpp和client.cpp的具体实现。Makefile用于编译项目,运行结果显示客户端和服务端的交互过程。

参考:
Thrift C++ 服务器和客户端开发实例–学习笔记
ostringstream的清空方法

代码:
ThriftLearnCode

下面是用Thrift C++编写的一个实例。记录学习下:

Thrift 版本:

# /usr/local/thrift/bin/thrift --version
Thrift version 0.10.0

Thrift命令:

/usr/local/thrift/bin/thrift -o ./thrift/ --gen cpp ./thrift/student.thrift

生成的文件:

student_constants.cpp
student_constants.h
StudentServ.cpp
StudentServ.h
StudentServ_server.skeleton.cpp
student_types.cpp
student_types.h

Server端代码:server.cpp

//server.cpp
#include "StudentServ.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>

#include <sstream>
#include "inifile.h"

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

using boost::shared_ptr;
using namespace std;


// 初始化配置信息
bool InitConfInfo(const std::string& file, int& port) {
    CIniFile ini;
    std::string str_val;
    if (!ini.Init(file)) {
        printf("ini init file failed: %s\n", file.c_str());
        return false;
    }

    // load config
    ini.Read_Profile_String("server", "port", str_val);
    if (!str_val.empty())
        port = atoi(str_val.c_str());
    else
        port = 8828;

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值