节俭C++服务器超时,Java 服务器不会

Thrift C++ server times out, java server doesn't

本文关键字:服务器 Java 超时 C++ 节俭      更新时间:2023-10-16

我正在尝试使用Apache thrift RPC框架在PHP客户端和c++服务器之间建立通信。经过几个小时毫无结果的调试,我从同一个节俭文件构建了一个java服务器,并使其正常工作。当我运行c++服务器时,没有调用我的任何方法,并且从java服务器获得响应的同一客户机抛出异常Exception: TSocket: timed out reading 4 bytes from localhost:65123(即使我已将客户机上的发送和接收超时设置为5秒)。至少这个错误是不同于错误时,我得到的服务器不运行[TSocket: Could not connect to localhost:65123 (Connection refused [111])],所以我知道c++服务器至少绑定到端口,客户端正在交谈。

(工作的)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++服务器代码片段都是从thrift编译器生成的框架代码中剪切和粘贴的。

我真的想不明白这个。为什么我的c++服务器不响应客户端?为什么我的处理程序(除了构造函数)中的方法都没有被调用?我将非常感谢社区提供的任何帮助。我使用的是thrift 0.9.0版本。下面是实现我的处理器的代码,如果它有帮助的话:
class EncabulationGameHandler : virtual public EncabulationGameIf {
 public:
  EncabulationGameHandler(InputListenerThreadStartupData * threadData) {
    // Your initialization goes here
  }
  int32_t RegisterPlayer() {
    // Your implementation goes here
    printf("RegisterPlayern");
  }
  void UnRegisterPlayer(const int32_t playerID) {
    // Your implementation goes here
    printf("UnRegisterPlayern");
  }
  bool IsGameRunning() {
    // Your implementation goes here
    printf("IsGameRunningn");
  }
  int32_t GetPlayerScore(const int32_t playerID) {
    // Your implementation goes here
    printf("GetPlayerScoren");
  }
  void Bounce(const int32_t playerID) {
    // Your implementation goes here
    printf("Bouncen");
  }
  void ChangeColor(const int32_t playerID) {
    // Your implementation goes here
    printf("ChangeColorn");
  }
};

你没有给我们足够的理由继续下去-所以看看差异,自己写一个最简单的c++服务器,你可以和你的独立java服务器一样。运行c++服务器(不在线程中)

c++代码看起来很像thrift框架中的自动生成的东西,所以我看不出这有什么错。

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