On Wed, Oct 20, 2010 at 3:28 AM, Paul <mjpabl...@gmail.com> wrote:
> Hi,
> I am sending messages from a C++ client to a Java server, but I am
> wondering about the length of the buffer I am using to send it over.
> This is the C++ code:
> *** the protocol buffers message is called snap1 ***
> int numBytesForDelim = sizeof(int);
> char *snap_buf2;
> snap_buf2 = new char[snap1.ByteSize() + 100];
> ZeroCopyOutputStream* raw_output = new ArrayOutputStream(snap_buf2,
> snap1.ByteSize()+numBytesForDelim);
>            CodedOutputStream* coded_output = new
> CodedOutputStream(raw_output);
> coded_output->WriteVarint32(snap1.ByteSize());
> snap1.SerializeToCodedStream(coded_output);
> delete coded_output;
> delete raw_output;
> send(socket, snap_buf2, snap1.ByteSize()+numBytesForDelim/2, 0);  //
> send over a socket
> delete [] snap_buf2;
> snap_buf2 = NULL;
> When I send this over to the Java server side, the Java code uses
> "parseDelimitedFrom" to parse the data.  however, I am not sure why
> when I call send, the length of the message has to be snap1.ByteSize()
> +numBytesForDelim/2 rather than snap1.ByteSize()+numBytesForDelim.  I
> would think that it would be the latter, but when I try it, I get
> exceptions on the Java side saying that the protocol message has not
> filled out all the fields.  For some reason, it is necessary for me to
> cut off the last two bytes when sending over TCP.  Any ideas as to why
> this is?

I dont know this in detail, but in my experience with java and c++ i
needed swap the two bytes before the buffer containing the length.
the length of the buffer is sent before the buffer from java to c++
using the java byte order.
I dont know if this applies, but you should print out the first two
bytes and try swapping them. Look up my old messages or code for

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to