thrift cpp hello world

WhatTime.thrift


namespace cpp roctime

 service TimeService {
i32 TellMeTime()
}


thrift -gem cpp WhatTime.thrift


mv TimeService_server.skeleton.cpp server.cpp


int32_t TellMeTime() {
// Your implementation goes here
time_t now_time = time(NULL);
return now_time;
}


g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o service 




client.cpp


#include "TimeService.h"
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
 
#include <iostream>
using namespace std;
 
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
 
using namespace roctime;
 
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));
 
time_t mytime = 0;
 
TimeServiceClient client(protocol);
transport->open();
mytime = client.TellMeTime();
 
cout << "Now is " << ctime(&mytime) << endl;
transport->close();
 
return 0;
}


g++ -Wall -I/usr/local/include/thrift *.cpp -lthrift -o client


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章