Kaiser4you
Kaiser4you

Reputation: 83

Google protocol buffers-Sending a message form C# client to a java Server

The Client sends a 1481 bytes array. The server can read all the 1481 bytes message without any problems but by parsing the given messsage from the received binary array i get this exeption:

com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).

The binary data is the same. I checked that I am using the right version of the proto files. I am a bit at a loss tbh. Any help appreciated.

Code

byte [] data= IOUtils.toByteArray(br1, "ASCII"); System.out.println("SIZE:" + data.length);
AddressBook adb1 = AddressBook.parseFrom(data); System.out.println("Server: Addressbook:" + adb1.getPersonCount()); System.out.println("Server: Addressbook:" + adb1.getPerson(0).getName());

Question:

I need to find a way to correctly parse the received Adressbook msg from the read 1481 bytes arry.

Thanks.

Upvotes: 1

Views: 2609

Answers (2)

Kaiser4you
Kaiser4you

Reputation: 83

Now it works I had same mistake by Serializing and Sending the Protocol Buffers Message

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501586

This is the problem:

br1 = new InputStreamReader(s.getInputStream());

That's trying to treat opaque binary data as text. It's not text, it's binary data. So when you convert that Reader into a byte array, you've lost a load of the original data - no wonder it's an invalid protocol buffer.

Just use:

AddressBook adb1 = AddressBook.parseFrom(s.getInputStream());

and avoid the lossy text conversion. That's assuming you haven't got something equally broken on the C# side, of course.

If you must go via text, you should use base64 encoding on both sides.

Upvotes: 2

Related Questions