以下代码有效。
服务器:
// SampleServiceHandler is a class that implements
// the Thrift service methods.
shared_ptr handler(new SampleServiceHandler);
shared_ptr processor(new SampleServiceProcessor(handler));
shared_ptr serverTransport(
new transport::TServerSocket(serverPort));
shared_ptr transportFactory(
new transport::TFramedTransportFactory());
shared_ptr protFactory(
new protocol::TBinaryProtocolFactory());
shared_ptr server(new server::TThreadedServer(
processor, serverTransport, transportFactory, protFactory));
server->serve();
客户:
<pre><code>shared_ptr<TSocket> socket(new TSocket(serverName, serverPort)); shared_ptr<TTransport> transport(new TFramedTransport(socket)); shared_ptr<protocol::TProtocol> protocol( new protocol::TBinaryProtocol(transport)); shared_ptr<ThriftClient> client(new SampleServiceClient(protocol)); transport->open(); client->sampleThriftMethod(); </code></pre>
但是,以前我在服务器端使用 TBufferedTransportFactory 而不是 TFramedTransportFactory 时它也能正常工作。我发现使用 TThreadedServer 的唯一示例代码使用 TBufferedTransportFactory,所以我想知道 TFramedTransportFactory 是否有问题。
我切换它的原因是因为我希望使用 TFramedTransport 与使用 Java TThreadedSelectorServer 的服务器对话的 Java 客户端也能够与这个使用 C++ TThreadedServer 的服务器对话。
最佳答案
TFramedTransport
在使用 TNonblockingServer
时在 C++ 库中是必需的(类似于 Java TThreadedSelectorServer)。
在其他情况下它是可选的,但它会起作用。它没有在示例中使用,那些正在使用 TThreadedServer。
仍然 - 您的设置没有任何问题。
关于java - C++ Thrift : Can I use TFramedTransportFactory with TThreadedServer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20532968/