c++ - 使用 C++ 客户端访问 C++ 服务器时出现问题

标签 c++ sockets rpc thrift

基于 C++ 的服务器 Something_server 有一个打印 ping 的方法

#include "Something.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

using boost::shared_ptr;

using namespace Test;
        class SomethingHandler : virtual public SomethingIf {
     public:
      SomethingHandler() {
        // Your initialization goes here
      }

      int32_t ping() {
        // Your implementation goes here
        printf("ping\n");
       return 0;
      }

    };

    int main(int argc, char **argv) {
      int port = 9090;
      shared_ptr<SomethingHandler> handler(new SomethingHandler());
      shared_ptr<TProcessor> processor(new SomethingProcessor(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;
    }

Something_client 应该调用此方法以打印出 "ping"

  #include "Something.h"  // As an example

#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

using namespace Test;

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));

  SomethingClient client(protocol);
  transport->open();
  client.ping();
  transport->close();

  return 0;
}

说明说“运行服务器并用客户端 ping 它”......不知道这是什么意思......

我愿意

 ./Something_server 

什么也没有发生......好像命令一直在运行而不是终止......所以我不太确定如何继续。

感谢所有帮助。

最佳答案

这意味着您应该首先运行 ./Something_server &(& 将作业置于后台,因此它不会使输出困惑)。 然后你运行 ./Something_client 显然会 ping 服务器。

关于c++ - 使用 C++ 客户端访问 C++ 服务器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6107187/

相关文章:

c++ - SFINAE成员(member)通过

java - telnet 关闭时 BufferedReader 或 Socket 不会引发异常

c - 关于 getservbyname 的正确答案是什么?

c++ - 通过 system() 调用启动另一个程序会阻塞套接字

go - Go 中的 RPC 有某种缓存?

c++ - typename参数包和auto参数包的区别?

c++ - 为 boost::property_maps 中的枚举专门化 boost::lexical_cast

ruby-on-rails - 如何在Rails中使用RabbitMQ实现RPC?

java - 从字符串变量 Java 进行转换

c++ - 如何从 MFC 应用程序调用函数的 Win32 API 版本?