zoukankan      html  css  js  c++  java
  • 【三】多语言互通

    ---恢复内容开始---

    # 【任务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)
    

    执行结果

    1. 首先启动server端:./server

    2. 然后执行client端:python client.py

    3. 结果:两端能够接受到互相发送的信息

    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可执行文件文件

    运行结果

    1. 首先启动server端:./server

    2. 然后启动编译后的client端:./client

    3. 结果:两端能够接受到互相发送的信息

    编译技巧(make操作)

    1. 创建名为:Makefile的文件

    2. 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
    
    1. 写入完成后就可以执行make命令来进行编译文件了,默认make编译server端,因为serverMakefile中写在前面,也可以选择编译,命令:make server或者make client

      ---恢复内容结束---

  • 相关阅读:
    TFS应用层服务器获取F5用户的真实IP地址(高可用性)
    安装TFS(2015)工作组模式代理服务器(Agent)
    Team Foundation Server 15 功能初探
    TFS 2013 生成(构建)历史记录保持策略(Retention Policy)
    TFS代码变更和工作项关联,为系统变更提供完美的跟踪轨迹
    修改TFS客户端的工作区类型
    比较TFS与SVN,你必须知道的10点区别
    数据字典
    查看源码 类图结构图(Eclipse + Idea)
    Mybatis对应的java和数据库的数据类型
  • 原文地址:https://www.cnblogs.com/screen/p/9481707.html
Copyright © 2011-2022 走看看