Linuxc/C ++ programming BASICS (24) Use thrift/RPC to develop simple instances (Continued 2)

Source: Internet
Author: User

Preface: the first two articles have described thrift/RPC installation and server writing. Here we add the compiling of the Client

1. Implement the client. cpp file as follows:

# Include "../Gen-CPP/musicservlet. H"
# Include <vector>
# Include <boost/shared_ptr.hpp>
# Include <thrift/protocol/tbinaryprotocol. h>
# Include <thrift/protocol/tprotocol. h>
# Include <thrift/transport/tsocket. h>
# Include <thrift/transport/tTransport. h>
# Include <thrift/transport/tbuffertransports. h>
Using namespace: Apache: thrift;
Using namespace: Apache: thrift: Protocol;
Using namespace: Apache: thrift: Transport;
Using namespace shansj;
Using boost: shared_ptr;
Int main (INT argc, char ** argv ){
Shared_ptr <tTransport> socket (New tsocket ("localhost", 5555 ));
Shared_ptr <tTransport> transport (New tbufferedtransport (socket ));
Shared_ptr <tprotocol> protocol (New tbinaryprotocol (Transport ));
Musicservletclient client (Protocol );
Try {
Transport-> open ();
STD: vector <music_info> music;
Music_info song;
Song. song_id = 99;
Song. song_name = "qilixiang ";
Song. song_singer = "zhoujielun ";
Song. song_album = "fantexi ";
Music. push_back (Song );
Client. sendmessage (music );
Transport-> close ();
} Catch (texception & Tx ){
Printf ("error: % s \ n", TX. What ());
}
Return 0;
}

The code is concise and clear.

Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8281815

II. The running result is as follows:

1. Client:


2. Server:



Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8281815

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.