Getting Started with Thrift (2)-Implementing a simple server/client application with thrift

Source: Internet
Author: User

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


This paper consists of 5 parts, of which 1, 2 and 3 are focused.


1. Preparation of [. Thrift] Files

You can Google to the official example as follows:

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

Save the above code 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 documents obtained are as follows:

Serv.cpp
Serv.h
Serv_server.skeleton.cpp
student_constants.cpp
student_constants.h
Student_types.cpp
Student_types.h

In Serv_server.skeleton.cpp, there is a 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 Client Programs

#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));

	Transport->open ();

	Your Codes

	transport->close ();

	return 0;
}

4. Compile/Link

g++-g-i/home/michael/opt/include/thrift-l/home/michael/opt/lib/-lthrift Serv.cpp student_types.cpp Student_ Constants.cpp serv_server.skeleton.cpp-o server

g++-g-i/home/michael/opt/include/thrift-l/home/michael/opt/ lib/-lthrift-lm-pthread-lz-lrt-lssl Serv.cpp student_types.cpp student_constants.cpp Client

5. Run

./server

./client


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.