Thrift C ++ server/client development

Source: Internet
Author: User

Content transferred from: http://blog.csdn.net/hbuxiaoshe/article/details/6558391

The original text is as follows:

I use C ++, so I will give a C ++ example to briefly introduce thrift's getting started.

The example is described as follows: the student information (student ID, name, gender, age) is sent to the server by the client.

To implement this example, we have to do the following:

(1) write the. Thrift File

(2) generate a CPP File

(3) Compile the client

(4) Compile and execute the CPP File

 

(1) write the. Thrift File

Student information is structured, so we can use thrift's struct. To achieve communication, we must use service.

The contents of the student. Thrift file written at the end are as follows:

Struct student {
1: i32 SnO,
2: String sname,
3: bool ssex,
4: I16 sage,
}
Service serv {
Void put (1: Student s ),
}

(2) generate a CPP File

Generating a CPP file is simple. You only need a thrift command:

/Home/xiaoshe/opt/bin/thrift-r -- Gen CPP student. Thrift

-- GEN: Specify the language to be generated. The generated CPP is stored in the Gen-CPP directory.

After the command is executed, the following files are generated in the./Gen-CPP/directory:

Serv. cpp

Serv. h

Serv_server.skeleton.cpp

Student_constants.cpp

Student_constants.h

Student_types.cpp

Student_types.h

Note the case sensitivity of the file:

Files starting with serv are generated by the service. This keyword is very important. We will also see classes starting with Serv.

Student is generated based on the name of the student. Thrift file.

These files can be compiled to generate the original server.

 

(3) Compile the client

After using the thrift command, we do not get the client source code we want, so the client program should be compiled and implemented by ourselves. Fortunately, we can use the following code snippet to write our client program:

[C-sharp]
View plaincopyprint?
  1. # Include "Serv. H" // replace it with your. h
  2. # Include <transport/tsocket. h>
  3. # Include <transport/tbuffertransports. h>
  4. # Include <protocol/tbinaryprotocol. h>
  5. Using namespace Apache: thrift;
  6. Using namespace Apache: thrift: Protocol;
  7. Using namespace Apache: thrift: Transport;
  8. Using boost: shared_ptr;
  9. Int main (INT argc, char ** argv ){
  10. Boost: shared_ptr <tsocket> socket (New tsocket ("localhost", 9090 ));
  11. Boost: shared_ptr <tTransport> transport (New tbufferedtransport (socket ));
  12. Boost: shared_ptr <tprotocol> protocol (New tbinaryprotocol (Transport ));
  13. Transport-> open ();
  14. // Write our code here
  15. Transport-> close ();
  16. Return 0;
  17. }

# Include "Serv. H "// <br/> Replace with your. h # include <transport/tsocket. h> # include <br/> <transport/tbuffertransports. h> # include <br/> <protocol/tbinaryprotocol. h> using namespace Apache: thrift; using <br/> namespace Apache: thrift: Protocol; using namespace <br/> Apache: thrift: Transport; using boost:: shared_ptr; int main (INT argc, <br/> char ** argv) {boost: shared_ptr <tsocket> socket (New <br/> tsocket ("localhost ", 9090); Boost: shared_ptr <tTransport> <br/> transport (New tbufferedtransport (socket); <br/> boost :: shared_ptr <tprotocol> protocol (New <br/> tbinaryprotocol (Transport); Transport-> open (); // write our code here <br/> transport-> close (); Return 0 ;}

Save as file client. cpp

(4) Compile and execute the CPP File

Compile server: G ++-g-I/home/xiaoshe/opt/include/thrift-L/home/xiaoshe/opt/lib/-lthrift Serv. CPP student_types.cpp student_constants.cpp serv_server.skeleton.cpp-O Server

Compile the client: g ++-g-I/home/xiaoshe/opt/include/thrift-L/home/xiaoshe/opt/lib/-lthrift-lm-pthread-LZ-LRT-lssl serv. CPP student_types.cpp student_constants.cpp client. CPP-O client

Run the server:./Server

Run the client:./client

 

(5) Transmit our data Student Information

The client has been connected to the server, but the server only has such a response (no more data to read), because there is no data interaction between the two.

We use the client as the sending end and modify client. cpp to send data to the server.

In "// write our code here"

Write down our code:

// Create a student variable first. Student is defined in student. thrift.

Student s;
S. Sno = 123;
S. sname = "xiaoshe ";
S. ssex = 1;
S. Sage = 30;

// Define another object client and a class starting with "serv"

Servclient client (Protocol );

// Call the put function to transmit data to the server. Put is a member function defined by student. Thrift using the service.

// After calling put, the server also calls the corresponding put ()
Client. Put (s );

 

 

The server is responsible for receiving data and making corresponding changes:

In put () of Class servhandler:

Printf ("Sno = % d sname = % s ssex = % d sage = % d/N", S. sno, S. sname. c_str (), S. ssex, S. SAGE );

 

Finally, compile and run the server. After the client is started, the server receives the message and the result is:

Put
Sno = 123 sname = xiaoshe ssex = 1 sage = 30

Now, the client can send data to the server.

I use C ++, so I will give a C ++ example to briefly introduce thrift's getting started.

The example is described as follows: the student information (student ID, name, gender, age) is sent to the server by the client.

To implement this example, we have to do the following:

(1) write the. Thrift File

(2) generate a CPP File

(3) Compile the client

(4) Compile and execute the CPP File

 

(1) write the. Thrift File

Student information is structured, so we can use thrift's struct. To achieve communication, we must use service.

The contents of the student. Thrift file written at the end are as follows:

Struct student {
1: i32 SnO,
2: String sname,
3: bool ssex,
4: I16 sage,
}
Service serv {
Void put (1: Student s ),
}

(2) generate a CPP File

Generating a CPP file is simple. You only need a thrift command:

/Home/xiaoshe/opt/bin/thrift-r -- Gen CPP student. Thrift

-- GEN: Specify the language to be generated. The generated CPP is stored in the Gen-CPP directory.

After the command is executed, the following files are generated in the./Gen-CPP/directory:

Serv. cpp

Serv. h

Serv_server.skeleton.cpp

Student_constants.cpp

Student_constants.h

Student_types.cpp

Student_types.h

Note the case sensitivity of the file:

Files starting with serv are generated by the service. This keyword is very important. We will also see classes starting with Serv.

Student is generated based on the name of the student. Thrift file.

These files can be compiled to generate the original server.

 

(3) Compile the client

After using the thrift command, we do not get the client source code we want, so the client program should be compiled and implemented by ourselves. Fortunately, we can use the following code snippet to write our client program:

[C-sharp]
View plaincopyprint?
  1. # Include "Serv. H" // replace it with your. h
  2. # Include <transport/tsocket. h>
  3. # Include <transport/tbuffertransports. h>
  4. # Include <protocol/tbinaryprotocol. h>
  5. Using namespace Apache: thrift;
  6. Using namespace Apache: thrift: Protocol;
  7. Using namespace Apache: thrift: Transport;
  8. Using boost: shared_ptr;
  9. Int main (INT argc, char ** argv ){
  10. Boost: shared_ptr <tsocket> socket (New tsocket ("localhost", 9090 ));
  11. Boost: shared_ptr <tTransport> transport (New tbufferedtransport (socket ));
  12. Boost: shared_ptr <tprotocol> protocol (New tbinaryprotocol (Transport ));
  13. Transport-> open ();
  14. // Write our code here
  15. Transport-> close ();
  16. Return 0;
  17. }

# Include "Serv. H "// <br/> Replace with your. h # include <transport/tsocket. h> # include <br/> <transport/tbuffertransports. h> # include <br/> <protocol/tbinaryprotocol. h> using namespace Apache: thrift; using <br/> namespace Apache: thrift: Protocol; using namespace <br/> Apache: thrift: Transport; using boost:: shared_ptr; int main (INT argc, <br/> char ** argv) {boost: shared_ptr <tsocket> socket (New <br/> tsocket ("localhost ", 9090); Boost: shared_ptr <tTransport> <br/> transport (New tbufferedtransport (socket); <br/> boost :: shared_ptr <tprotocol> protocol (New <br/> tbinaryprotocol (Transport); Transport-> open (); // write our code here <br/> transport-> close (); Return 0 ;}

Save as file client. cpp

(4) Compile and execute the CPP File

Compile server: G ++-g-I/home/xiaoshe/opt/include/thrift-L/home/xiaoshe/opt/lib/-lthrift Serv. CPP student_types.cpp student_constants.cpp serv_server.skeleton.cpp-O Server

Compile the client: g ++-g-I/home/xiaoshe/opt/include/thrift-L/home/xiaoshe/opt/lib/-lthrift-lm-pthread-LZ-LRT-lssl serv. CPP student_types.cpp student_constants.cpp client. CPP-O client

Run the server:./Server

Run the client:./client

 

(5) Transmit our data Student Information

The client has been connected to the server, but the server only has such a response (no more data to read), because there is no data interaction between the two.

We use the client as the sending end and modify client. cpp to send data to the server.

In "// write our code here"

Write down our code:

// Create a student variable first. Student is defined in student. thrift.

Student s;
S. Sno = 123;
S. sname = "xiaoshe ";
S. ssex = 1;
S. Sage = 30;

// Define another object client and a class starting with "serv"

Servclient client (Protocol );

// Call the put function to transmit data to the server. Put is a member function defined by student. Thrift using the service.

// After calling put, the server also calls the corresponding put ()
Client. Put (s );

 

 

The server is responsible for receiving data and making corresponding changes:

In put () of Class servhandler:

Printf ("Sno = % d sname = % s ssex = % d sage = % d/N", S. sno, S. sname. c_str (), S. ssex, S. SAGE );

 

Finally, compile and run the server. After the client is started, the server receives the message and the result is:

Put
Sno = 123 sname = xiaoshe ssex = 1 sage = 30

Now, the client can send data to the server.

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.