参考:
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;