Thrift Small Test--c++

Source: Internet
Author: User

"Turn from" http://blog.csdn.net/poechant/article/details/6618284#

Thrift can be used in C + +, Java, Python, and other languages automatically generated, here in C + + as an example.

1. Writing [. Thrift] Files

struct student{   1:i32 Sno,   2:string sname,   3:bool ssex,   4:i16 sage,  }  service serv{   void Put (1:student s),  

  

The above code is saved as a student.thrift file.

2. Automatically generate server-side programs

Enter the following command in terminal to automatically generate [. cpp] and [. h] files.

Thrift-r--gen CPP Student.thrift

The resulting files are as follows:

    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

Where Serv_server.skeleton.cpp has the main function running on the server side. The Serv and student of these file names are related to the thrift file you originally created.

3. Writing the client program

#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::p rotocol;  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. Compiling/linking

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 serverg++-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. Running

./server

./client

Thrift Small Test--c++

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.