访问 C++ 时出现问题带有 C++ 的服务器客户

发布于 2024-11-09 22:44:18 字数 2358 浏览 1 评论 0原文

基于 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 

但没有任何反应...就好像命令永远运行而不终止...所以我不太确定如何继续。

感谢所有帮助。

C++ based Server Something_server has a method that prints 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 is supposed to call this method in order to print out "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;
}

instructions say "run the server and ping it with client"....no clue what this means...

I do

 ./Something_server 

and nothing happens....as if the command were running forever and not terminating...so I am not quite sure how to proceed.

All help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-11-16 22:44:18

这意味着您应该首先运行 ./Something_server & (& 将作业置于后台,这样就不会扰乱输出)。
然后你运行 ./Something_client 显然会 ping 服务器。

It means that you should first run ./Something_server & (& puts the job in background so it doesnt clutter output).
Then you run ./Something_client which apparently pings the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文