Thrift Windows vs2013 Test Example

Source: Internet
Author: User

first you need to compile thrift's compiler

Source code directory in:

\thrift-master\compiler\cpp

The original is vs2010 function, with vs2013 Open will prompt upgrade, because this is just a tool, so do not need to upgrade, directly compiled to get the required EXE


After compiling a thrift.exe, this tool can generate the corresponding target code for the intermediate code

generate the required test code

Create a new Notepad in the Thrift.exe directory, paste the following, and then change the file name to Student.thrift:

<span style= "FONT-SIZE:18PX;" >struct student{i32 sno,string sname,bool ssex,i16 sage,}service serv{Void put (1:student s),}</span>

Enter the following command in the terminal:

<span style= "FONT-SIZE:18PX;" >thrift.exe-r--gen CPP Student.thrift</span>


Get the folder Gen-cpp we need, which is the code generated by the Thrfit command




Create a new server project in Solution Thrift

Copy the folder Gen-cpp in the past, the folder directory is composed of the following:



The. h and. cpp in Gen-cpp are introduced in the Engineering catalog:



Copy the contents of the. cpp file in the red box above into Server.cpp, then remove it and add the corresponding. lib:



Because you intend to print the content that the client sends on the server side, the contents of the corresponding put method are modified in the default Servhandler:

printf ("sno=%d sname=%s ssex=%dsage=%d/n", S.sno, S.sname.c_str (), S.ssex, s.sage);



In the project to set the corresponding header file directory, you can refer to my:

Gen-cpp, .... \CPP\SRC, .... \cpp\openssl-1.0.1e;d:\boost_1_57_0;

Library Directory:

D:\boost\boost_1_57_0\bin\vc12-x64\lib;



again, create a new client project.

The same can be referred to 3, also need to remove

Copy the folder Gen-cpp in the past and copy the CppClient.cpp in the \thrift-master\tutorial\cpp directory.

The folder directory consists of the following:



The project catalog is as follows:



Delete the Serv_server.skeleton.cpp in the project and add the following code to the Clien.cpp:

#include <thrift/transport/TSocket.h> #include "Serv.h" #include <thrift/protocol/tbinaryprotocol.h># Include <thrift/server/TSimpleServer.h> #include <thrift/transport/TServerSocket.h> #include <thrift /transport/tbuffertransports.h> #include <string>using namespace:: Apache::thrift;using namespace:: Apache: : Thrift::p rotocol;using Namespace:: Apache::thrift::transport;using namespace:: Apache::thrift::server;using boost: : shared_ptr; #pragma comment (lib, "... /lib/x64/libthrift.lib ") #pragma comment (lib,". /lib/x64/libthriftnb.lib ") #pragma comment (lib,". /lib/x64/libeay32.lib ") #pragma comment (lib,". /lib/x64/ssleay32.lib ") int main (int argc, char** argv) {int port = 9090;shared_ptr<ttransport> socket (New Tsocket ( "127.0.0.1", 60010));shared_ptr<ttransport> Transport (new Tbufferedtransport (socket));shared_ptr< Tprotocol> Protocol (new Tbinaryprotocol (transport)); Servclient Client (protocol); Student S;s.sno = 123;s.sname = "Zengraoli"; s.ssex = 1;S.sage = 30;try{transport->open (); Client.put (s); Transport->close ();} catch (texception& tx) {printf ("error:%s\n", Tx.what ());} GetChar (); return 0;}

In the project to set the corresponding header file directory, you can refer to my:

Gen-cpp, .... \SRC, .... \cpp\openssl-1.0.1e;d:\boost_1_57_0;

Library Directory:

D:\boost\boost_1_57_0\bin\vc12-x64\lib;


Start Testing

First open the server to run:


Then open the client, the server message is as follows:



entire project (including Lib library, test example, Libevent, OpenSSL)

http://download.csdn.net/detail/zengraoli/9510383



Thrift Windows vs2013 Test Example

Related Article

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.