• thrift小试--C++


    【转自】http://blog.csdn.net/poechant/article/details/6618284#

    Thrift可以实现C++、JavaPython等多种语言的自动生成,此处以C++为例。

    1. 编写[.thrift]文件

    将代码保存为student.thrift文件。

    struct Student{  
     1: i32 sno,  
     2: string sname,  
     3: bool ssex,  
     4: i16 sage,  
    }  
    
    service Serv{  
     void put(1: Student s),  
    } 
    

      

    2. 自动生成服务器端程序

    在Terminal中输入如下命令,可自动生成[.cpp]和[.h]文件。

     

    thrift -r --gen cpp student.thrift  

    得到的文件如下:

     
    1. Serv.cpp  
    2. Serv.h  
    3. Serv_server.skeleton.cpp  
    4. student_constants.cpp  
    5. student_constants.h  
    6. student_types.cpp  
    7. student_types.h  

    其中Serv_server.skeleton.cpp中有服务器端运行的main函数。这些文件名的Serv和student与你最初创建的thrift文件有关。

    3. 编写客户端程序

    #include "Serv.h"  // Your .h File  
    #include <transport/TSocket.h>  
    #include <transport/TBufferTransports.h>  
    #include <protocol/TBinaryProtocol.h>  
    
    using namespace apache::thrift;  
    using namespace apache::thrift::protocol;  
    using namespace apache::thrift::transport;  
    using boost::shared_ptr;  
    
    int main(int argc, char **argv) {  
    
         boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));  
         boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));  
         boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));  
    
         ::SS::Student stu;
         ::SS::ServClient sn(protocol);
         transport->open();
         sn.put(stu);
         transport->close();  
         return 0;  
    
    } 
    

      

    4. 编译/链接

    g++ -g -I/usr/include/thrift -L/usr/lib/ -lthrift -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H  Serv.cpp student_types.cpp student_constants.cpp Serv_server.skeleton.cpp -o server
    
    g++ -g -I/usr/include/thrift -L/usrlib/ -lthrift -lm -pthread -lz -lrt -lssl -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H Serv.cpp student_types.cpp student_constants.cpp client.cpp -o client
    

      

    5. 运行

     

    ./server  

    ./client  

  • 相关阅读:
    mvc+struct1+struct2
    JSP中动态include与静态include的区别
    村上春树的经典语录合集
    50. 数组剔除元素后的乘积
    46. 主元素
    Centos7:yum安装MySQL5.7后如何设置root密码
    产品经理人的持续交付和DevOps实践
    利用jenkins实现自动构建、部署,提升团队开发效率
    基于lua-nginx-module(openresty)的WEB应用防火墙
    Nginx+Lua+MySQL/Redis实现高性能动态网页展现
  • 原文地址:https://www.cnblogs.com/dorothychai/p/6783592.html
Copyright © 2020-2023  润新知