2013-04-25 95 views
0

我正在尝试使用Apache thrift RPC框架在PHP客户机和C++服务器之间建立通信。经过几个小时的无果调试之后,我从同一个节俭文件构建了一个java服务器,并开始工作。当我运行C++服务器时,我的任何方法都不会被调用,并且从java服务器获得响应的同一客户端会抛出异常Exception: TSocket: timed out reading 4 bytes from localhost:65123(即使我已将客户端上的传输和接收超时设置为5秒。 )至少这个错误与我在服务器没有运行时得到的错误不同[],所以我知道C++服务器至少绑定到客户端正在与之通话的端口。Thrift C++服务器超时,java服务器不支持

的(工作)的Java服务器代码是:

public class Server 
{ 
    public static void Start(EncabulationGame.Processor<EncabulationInputListener> processor) 
    { 
     try 
     { 
      TServerTransport serverTransport = new TServerSocket(65123); 
      TServer server = new TSimpleServer(new Args(serverTransport).processor(processor)); 
      System.out.println("Starting the simple server..."); 
      server.serve(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 


    public static void main(String[] args) 
    { 
     Start(new EncabulationGame.Processor<EncabulationInputListener>(new EncabulationInputListener())); 
    } 

} 

的(非工作)C++服务器在线程产生从我的应用程序的主处理线程分离。代码看起来像:

void* ListenerThreadEntryPoint(void* threadStartData) 
{ 
    struct InputListenerThreadStartupData * threadData; 
    threadData = ((struct InputListenerThreadStartupData *) threadStartData); 
    int port = threadData->ListnerThreadPort; 

    shared_ptr<EncabulationGameHandler> handler(new EncabulationGameHandler(threadData)); 
    shared_ptr<TProcessor> processor(new EncabulationGameProcessor(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; 
} 

Java和C++的服务器代码片段剪切并粘贴从储蓄机构编译器生成的骨架代码。

我真的不知道这一点。为什么我的C++服务器不响应客户端?为什么我的处理程序中的所有方法(构造函数除外)都被调用?我非常感谢社区可以提供的任何帮助。我正在使用节俭0.9.0构建。

下面是实现我的处理程序,如果它有助于代码:

class EncabulationGameHandler : virtual public EncabulationGameIf { 
public: 
    EncabulationGameHandler(InputListenerThreadStartupData * threadData) { 
    // Your initialization goes here 
    } 

    int32_t RegisterPlayer() { 
    // Your implementation goes here 
    printf("RegisterPlayer\n"); 
    } 

    void UnRegisterPlayer(const int32_t playerID) { 
    // Your implementation goes here 
    printf("UnRegisterPlayer\n"); 
    } 

    bool IsGameRunning() { 
    // Your implementation goes here 
    printf("IsGameRunning\n"); 
    } 

    int32_t GetPlayerScore(const int32_t playerID) { 
    // Your implementation goes here 
    printf("GetPlayerScore\n"); 
    } 

    void Bounce(const int32_t playerID) { 
    // Your implementation goes here 
    printf("Bounce\n"); 
    } 

    void ChangeColor(const int32_t playerID) { 
    // Your implementation goes here 
    printf("ChangeColor\n"); 
    } 

}; 

回答

0

回答我自己的问题,我设法通过使用多线程Thrift服务器模型来解决这个问题。我从来没有弄清楚为什么上面发布的代码片段不起作用。

0

你没有给我们足够的去 - 这样看的差异,并自己编写简单的C++服务器,你可以是这样的与你的独立Java相同(例如,他没有在一个线程中的C++服务器)

C++代码看起来很像从节俭框架中自动生成的东西,所以我看不出如何可能是错误的。