LinuxC/C++编程基础(23) 使用thrift/rpc开发简单实例(续1)
来源:互联网 发布:网络段子写手招聘 编辑:程序博客网 时间:2024/05/17 23:54
写在前面:thrift/rpc的安装请参考前一篇文字,这一篇文字主要叙述thrift/rpc的使用
一.定义自己的服务接口,如下:
1.music.thrift文件内容如下:
namespace cpp shansj
struct Music_Info{
1: i32 song_id;
2: string song_name;
3: string song_singer;
4: string song_album;
}
说明:这里只是示意地定义了一个结构体,然后希望通过客户端调用,传递这样一个结构体给服务器端
2.定义服务接口,如下:
song_rpc.thrift文件的内容如下:
namespace cpp shansj
include "music.thrift"
service MusicServlet{
void sendMessage(1: list<music.Music_Info> music);
}
转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8281508
3.编译生成代码,如下:
thrift --gen cpp music.thrift
thrift --gen cpp song_rpc.thrift
在gen-cpp文件夹下会生成相应的文件,如下:
二.Server端的代码编写,如下:
#include <vector>
#include <boost/shared_ptr.hpp>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include "../gen-cpp/MusicServlet.h"
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace shansj;
using boost::shared_ptr;
class MusicServletHandler:virtual public MusicServletIf{
public:
MusicServletHandler(){
}
void sendMessage(const std::vector<Music_Info>& music){
printf("[MusicServletHandler::sendMessage] invoke\n");
printf("song_id:%d\n",music[0].song_id);
printf("song_name:%s\n",music[0].song_name.c_str());
printf("song_singer:%s\n",music[0].song_singer.c_str());
printf("song_album:%s\n",music[0].song_album.c_str());
}
};
说明:1.该类中sendMessage方法只是示意地,简单的把收到的客户端发过来的数据打印出来
2.服务接口中定义的方法,在服务器端必须全部实现,具体原因,可以看一下产生的代码就可以知道
int main(int argc,char** argv){
int port = 5555;
shared_ptr<MusicServletHandler> handler(new MusicServletHandler());
shared_ptr<TProcessor> processor(new MusicServletProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor,serverTransport,transportFactory,protocolFactory);
server.serve();
return 0;
}
三.运行结果,如下:
转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8281508
- LinuxC/C++编程基础(23) 使用thrift/rpc开发简单实例(续1)
- LinuxC/C++编程基础(24) 使用thrift/rpc开发简单实例(续2)
- LinuxC/C++编程基础(31) 使用thrift/rpc开发简单实例(续3)
- LinuxC/C++编程基础(32) 使用thrift/rpc开发简单实例(续4)
- LinuxC/C++编程基础(22) 使用thrift/rpc开发简单实例
- LinuxC/C++编程基础(21) 使用boost::asio搭建服务器简单实例(续)
- LinuxC/C++编程基础(20) 使用boost::asio搭建服务器简单实例
- LinuxC/C++编程基础(7) boost::mutex的简单实例
- LinuxC/C++编程基础(13) shell脚本简单实例
- LinuxC/C++编程基础(35) std::istream使用实例
- LinuxC编程之 linux简单基础(1)
- thrift 简单安装以及rpc使用心得
- linuxc编程一站式学习(c基础)
- RPC-Thrift简单应用
- thrift(1)------基于thrift通信组件的简单RPC服务
- LinuxC/C++编程基础(34) 虚函数编程实例
- linuxC编程基础
- LinuxC基础编程
- AndroidManifest.xml文件详解(receiver)
- windows8开发-xaml中实现ComboBox的Item样式
- 遇到xxx is not translated in yyy, zzz的问题。
- php笔记之GD库图片创建/简单验证码
- 电商如何对商品进行价格定位?(一)
- LinuxC/C++编程基础(23) 使用thrift/rpc开发简单实例(续1)
- 多文件上传
- iOS图标尺寸一览
- backupme.sh
- tftp-hpa ArchLinux开tftp %Error opening tftp:(No such file or directory)
- HDU 1098
- uC/OS-II学习笔记(2012.12.11)任务就绪表
- ant部分常用命令
- C#实现FTP上传下载功能