thrift_demo

student.thrift

struct Student {
        1:i32 id,
        2:string name,
        3:i16 age,
}


service PutStudentInfoService {
        void put(1:Student s);
}

serv


server.cpp

#include<iostream>
#include "PutStudentInfoService.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>


using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;


using boost::shared_ptr;
using namespace std;
//using namespace  ;


class PutStudentInfoServiceHandler : virtual public PutStudentInfoServiceIf {
 public:
  PutStudentInfoServiceHandler() {
    // Your initialization goes here
  }


  void put(const Student& s) {
    
    cout<<"服务器即将存储客户端传来的信息: id="<<s.id<<"   name="<<s.name<<"   age="<<s.age<<endl;
	students.push_back(s);
	cout<<"现在总共有 "<<students.size()<<"个学生的信息!  分别为:"<<endl;
	for(vector<Student>::iterator it=students.begin(); it!=students.end();it++)
	    cout<<"id="<<s.id<<"   name="<<s.name<<"     age="<<s.age<<endl;
  }
private:
	vector<Student> students;


};


int main(int argc, char **argv) {
  int port = 9090;
  shared_ptr<PutStudentInfoServiceHandler> handler(new PutStudentInfoServiceHandler());
  shared_ptr<TProcessor> processor(new PutStudentInfoServiceProcessor(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);

// TThreadedServer server(processor, serverTransport, transportFactory, protocolFactory);
/*
 shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(15);
  shared_ptr<PosixThreadFactory> threadFactory 
  = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());


  threadManager->threadFactory(threadFactory);
  threadManager->start();
  printf("start.../n");


TThreadPoolServer server(processor,serverTransport,transportFactory, protocolFactory, threadManager);
*/

  server.serve();
  return 0;
}



client.cpp

#include"PutStudentInfoService.h"
#include<transport/TSocket.h>
#include<transport/TBufferTransports.h>
#include<protocol/TBinaryProtocol.h>
#include<iostream>


using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;


int main(int argc ,char **argv){
	boost::shared_ptr<TSocket> socket(new TSocket("localhost" ,9090));
	boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
	boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
	
	PutStudentInfoServiceClient client(protocol);
	transport ->open();


	Student s;
	s.id=201031003100;
	s.name="chenwei";
	s.age=23;
	
	cout<<"客户端即将要向服务器提交的信息为: id="<<s.id<<"   name="<<s.name<<"   age="<<s.age<<endl;
	client.put(s);
	transport ->close();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值