"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:
- Serv.cpp
- Serv.h
- Serv_server.skeleton.cpp
- Student_constants.cpp
- Student_constants.h
- Student_types.cpp
- 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++