LinuxC/C++编程基础(24) 使用thrift/rpc开发简单实例(续2)

来源:互联网 发布:百度淘宝推广怎么收费 编辑:程序博客网 时间:2024/05/21 06:41

写在前面:前面两篇文字已经把thrift/rpc的安装以及服务端的编写叙述了,这里再把客户端的编写加上

一.client.cpp文件实现,如下:

#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;
}

代码简洁明了,不再赘述

转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8281815

二.运行结果,如下:

1.客户端:


2.服务端:



转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8281815


原创粉丝点击