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?
- # Include "Serv. H" // replace it with your. h
- # 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 ));
- Transport-> open ();
- // Write our code here
- Transport-> close ();
- Return 0;
- }
# 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?
- # Include "Serv. H" // replace it with your. h
- # 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 ));
- Transport-> open ();
- // Write our code here
- Transport-> close ();
- Return 0;
- }
# 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.