公司的代码之前实现了thrift客户端,服务端单向通信,也就是在一个连接中,只能客户端调用服务端的接口,而服务端不能调用客户端的接口。需要修改为是双向通信,也就是允许服务端主动给客户端发消息,或者说调用客户端的接口。
下面的代码是服务端部分代码:
nbSocket.reset(new transport::TNonblockingServerSocket(port));
serverThread = threadFactory->newThread(stdcxx::shared_ptr<TServer>(
new TNonblockingServer(getprocessorfactory(johnson, connections), protocolFactory, nbSocket, threadManager)));
服务端使用了TNonblockingServer的服务模型。
再来看一下客户端的连接代码:
stdcxx::shared_ptr<TTransport> socket(new TSocket("localhost", 9091));
stdcxx::shared_ptr<TTransport> transport(new TFramedTransport(socket));
stdcxx::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
由于服务端使用的服务模型,客户端的数据传输方式只能用TframedTransport。
由此,当服务端调用客户端接口发送数据时就会抛出如下异常: