---恢复内容开始---
# 【任务3】多语言互通说明
client:python
server:c++
server端
命令:thrift --gen cpp RecSys.thrift
自动生成gen-cpp
目录,这个目录下的代码仅完成server功能
进入gen-cpp
目录之后编译server端,命令:g++ -g -Wall -I./ -I/usr/local/include/thrift RecSys.cpp RecSys_constants.cpp RecSys_types.cpp RecSys_server.skeleton.cpp -L/usr/local/lib/*.so -lthrift -o server
gen-cpp/RecSys_server.skeleton.cpp
文件中修改的内容:
void rec_data(std::string& _return, const std::string& data) {
// Your implementation goes here
printf("=======================
");
std::cout << "receive client data:" << data << std::endl;
std::string ack = "yeah,I love you too!!";
_return = ack;
}
client端(python)
-
创建py文件:
client.py
-
代码:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.path.append("gen-py")
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from RecSys import RecSys
# from demo.ttypes import *
try:
# Make Socket
# 建立socket, IP 和port要写对
transport = TSocket.TSocket('localhost', 9090)
# Buffering is critical. Raw sockets are very slow
# 选择传输层,这块要和服务器的设置一样
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
# 选择传输协议,这个也要和服务器保持一致,负责无法通信
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = RecSys.Client(protocol)
# Connect!
transport.open()
# Call server services
rst = client.rec_data("I love you!")
print rst
# close transport
transport.close()
except Thrift.TException, ex:
print "%s" % (ex.message)
执行结果
-
首先启动server端:
./server
-
然后执行client端:
python client.py
-
结果:两端能够接受到互相发送的信息
client端(C++)
-
创建c++文件:
client.cpp
-
第一版代码:
#include "RecSys.h"
#include <iostream>
#include <string>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
#include <fcgi_stdio.h>
#include <fcgiapp.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;
using std::string;
using boost::shared_ptr;
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));
transport->open();
RecSysClient client(protocol);
string send_data = "I love you!!!";
string receive_data;
client.rec_data(receive_data,send_data);
cout << "receive server data: " << receive_data << endl;
transport->close();
return 0;
}
-
编译c++版的client:
命令:编译client: g++ -g -Wall -I./ -I/usr/local/include/thrift RecSys.cpp client.cpp -L/usr/local/lib/*.so -lthrift -o client
-
完成之后会自动生成
client
可执行文件文件
运行结果
-
首先启动server端:
./server
-
然后启动编译后的client端:
./client
-
结果:两端能够接受到互相发送的信息
编译技巧(make操作)
-
创建名为:
Makefile
的文件 -
在
Makefile
文件中写入以下内容:
server:
g++ -g -Wall -I./ -I/usr/local/include/thrift RecSys.cpp RecSys_constants.cpp RecSys_types.cpp RecSys_server.skeleton.cpp -L/usr/local/lib/*.so -lthrift -o server
client:
g++ -g -Wall -I./ -I/usr/local/include/thrift RecSys.cpp client.cpp -L/usr/local/lib/*.so -lthrift -o client
- 写入完成后就可以执行
make
命令来进行编译文件了,默认make
编译server
端,因为server
在Makefile
中写在前面,也可以选择编译,命令:make server
或者make client
---恢复内容结束---