C++ thrift client: how to send buffer without length?

1k views Asked by At

I have my c++ client application and Java server. The client app connects and send data to the server.

For example I send integer value:

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

The server side code is:

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

The question is: When I added one more read integer (see my comment) it works ok. First of all i read the length (4 bytes) and then data - integer.

It means that the client sends length (int) data (int)

but server reads only data.

How I can send data only? I can use another thrift transport or protocol or use non-thrift library...

3

There are 3 answers

0
Ericwang On

But if you use non-blocking sever ,then you must use TFramededTransport

1
ZedZip On

To use TBufferedTransport instead TFramededTransport because TFramedTransport adds (!) length before buffer.

0
JensG On

The more general statement is, that the client-side Thrift protocol/transport stack must match the server-side, otherwise you are very likely to get in trouble.

E.g. if the server uses binary protocol, framed and multiplex transport, the client has to do exactly the same.