我有我的C++客户端应用程序和Java服务器。 客户端应用程序连接并将数据发送到服务器。C++ thrift客户端:如何发送缓冲区没有长度?
比如我送整数值:
boost::shared_ptr<TSocket> socket(new TSocket(hostMS, portMS));
boost::shared_ptr<TFramedTransport> transport(new TFramedTransport(socket));
boost::shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
protocol->writeI32(0xCA12EEAA);
transport->writeEnd();
transport->flush();
服务器端代码:
int nMagic =0;
// int nLen = in.readInt(); <------------here is I read the length
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
nMagic = ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
的问题是: 当我增加了一个读整数(见我的评论),它的工作原理好。首先我读长度(4字节),然后是数据 - 整数。
这意味着,客户端发送 长度(INT) 数据(INT)
但服务器只读取数据。
我该如何发送数据?我可以使用另一个节俭运输或协议或使用非节俭库...
如何将对方知道有多少数据没有length属性看? –
嗯..但它的事实。服务器不读取它。可能是之前在基本Java类中读过的。我做了另一个实验:运行套接字测试工具Hercules(www.hw-group.com)并连接到服务器,然后发送十六进制值 - 服务器成功接收它。 – Oleg