Use of Thrift under Windows (C + +)

Source: Internet
Author: User

Thrift CPP Environment Construction:

1, installation boost_1_53_0, note that when using the VS2010 version, using the binary boost installation version, the generated Lib may be, in subsequent operations will be problematic. In the source directory, run Bootstrap.dat, and then generate the B2.exe file, which compiles the required library for boost, generates the file under the Boost\stage\lib file, and uses that path for subsequent additions to Lib.

2, the network download Win64openssl_light-1_1_0f.exe, Win64openssl-1_1_0f.exe, you can choose to install the latter, the former is a lightweight non-source of the target file.

3, download thrift-0.9.0 source code and editor. Open the Thrift-0.9.0\thrift-0.9.0\lib\cpp in the SLN project, compile the Libthrift engineering and LIBTHRIFTNB Engineering. The former is blocked, the latter is a non-blocking mode of the server, the non-blocking mode of the server requires Libevent library.

4, to compile the Libthrift project, add the dependent header file (c + +-general–additional include Directories):

C:\local\boost_1_53_0

C:\OpenSSL-Win64\include

To add a dependent link library: (Linker–general–additional library Directories)

C:\local\boost_1_53_0\stage\lib

Build the project, the subsequent required libthrift.lib files are generated in the Debug folder.

5, for the well-written interface file Helloworld.thrift, through the command line to generate the corresponding interface files, files in the Gen-cpp, which contains HelloWorld_server.skeleton.cpp for the server framework.

6. Create an empty project through VS2010, add the header file and resource file in Gen-cpp, add the dependent header file of Boost library and the Lib method as above, add thrift related file as:

Include Item: C:\USERS\ADMINISTRATOR\DESKTOP\THRIFT-0.9.0\THRIFT-0.9.0\LIB\CPP\SRC

LIB Entry: C:\Users\Administrator\Desktop\thrift-0.9.0\thrift-0.9.0\lib\cpp\Debug

At this point, the compilation may appear "Error LNK2019", unresolved external symbols, the problem due to the lack of the corresponding library files, the workaround is: linker–input–additional dependencies add Libthrift.lib.

The compilation process may appear the statement error, for example will have: using Namespace; This statement can be commented out.

7, this time can be compiled through, but error:

In this case, add twinsocksingleton::create () to the main function in the CPP that generated the server framework; It is no longer necessary to have this sentence in the high version of Thrift.

Run again.

Open the previous Python client, set the port, and run to see the results of the communication on the server.

The authoring of the server can be modified under the generated framework, and the client is written as:

#include "HelloWorld.h" #include <thrift\transport\TSocket.h> #include <thrift\transport\ tbuffertransports.h> #include <thrift\protocol\TBinaryProtocol.h> #include <thrift\server\ tsimpleserver.h> #include <thrift\transport\tserversocket.h>//#include <thrift\transport\ Tbuffertransports.h>using namespace apache::thrift;using namespace Apache::thrift::p rotocol;using namespace Apache::thrift::transport;using boost::shared_ptr; #include <iostream>int main (int Argc,char **argv) {shared_ Ptr<tsocket> socket (New Tsocket ("127.0.0.1", 9090));shared_ptr<ttransport> Transport (new Tbufferedtransport (socket));shared_ptr<tprotocol> Protocol (new Tbinaryprotocol (transport)); Helloworldclient Client (protocol); Try{transport->open ();} catch (ttransportexception) {transport->close ();} Client.ping (); std::string S;client.sayhello (s); Std::cout<<s<<std::endl;getchar (); return 0;}

  

As above, where the return value of sting needs to modify the SayHello corresponding function in the server, so that s gets a return value.

The client project also needs to be added to the library, and the files in the Gen-cpp (except the server framework file) are copied to the directory and added to the project.

Thrift uses the C/S model, does not support bidirectional communication, in order to solve this problem, usually in the communication between the two sides to establish two communication channels, open two ports.

Use of Thrift under Windows (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.