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

本文详细介绍了如何使用 Thrift 构建 RPC 客户端,包括代码实现、运行结果展示及注意事项。通过实例演示了如何发送消息到服务端,并接收响应,对于理解 Thrift 在远程调用中的应用提供了实用指南。

写在前面:前面两篇文字已经把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.youkuaiyun.com/linyanwen99/article/details/8281815

二.运行结果,如下:

1.客户端:


2.服务端:



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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值