You're giving Java a 4096-byte buffer, even though there are only 33
bytes. The parser gets confused by the trailing 0's. A protocol
message does not encode, itself, how long it is. If you know that it
doesn't fill the whole byte array, you should use
bla.newBuilder().mergeFrom(byte[], off, len) or truncate the byte
buffer yourself.
On Wed, Dec 5, 2012 at 10:37 AM, valadas <
valadas...@gmail.com> wrote:
> Hello I'm trying to use protobuf 2.4.1 to exchange messages between c++ and
> java.
>
> I have defined a .proto file (attached) with two union messages ,one to be
> sent from java to c++ and other the other way around.
>
> I started testing the c++ but I'm not able to receive the message in the
> java side (I got an exception about a zero tag)
>
> The C++\QT code is as follows:
>
>
> char buf[4096];
>
> operational_gui::OperationalMessage message;
> message.set_type(operational_gui::OperationalMessage::OWNPOSITIONREPORT);
> message.mutable_oprep()->set_latitude(38.5);
> message.mutable_oprep()->set_longitude(-9.8);
> message.mutable_oprep()->set_heading(25);
> message.mutable_oprep()->set_validity(0);
>
>
> message.SerializeToArray(buf, message.ByteSize());
> m_pSocket->write(buf, message.ByteSize());
> m_pSocket->flush();
>
> the m_pSocket is a QLocalSocket
>
> on the java I have the following code:
>
> FileinputStream pipe = new FileinputStream(pipeName);
> char buffer = new char[4096];
>
> pipe.read(buffer);
>
> MessagesProtos.OperationalMessage message =
> MessagesProtos.OperationalMessage.parseFrom(buffer);
>
> switch(message.getType()){
> case OWNPOSITIONREPORT:
> System.out.println("Response is OwnPositionReport");
> break;
> case FRIENDPOSITIONREPORT:
> System.out.println("Response is FriendPositionReport");
> break;
> case COMMSTATUSREPORT:
> System.out.println("Response is CommStatusReport");
> break;
> }
>
>
> But parseFrom always give an exception stating "Protocol message contained
> an invalid tag (zero)"
>
> The buffer sent is the same that is received on java (33 bytes, see
> attachment)
>
> Any hints ?
>
> Thanks in advance
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/protobuf/-/dvVqzdUPecgJ.
> To post to this group, send email to
prot...@googlegroups.com.
> To unsubscribe from this group, send email to
>
protobuf+u...@googlegroups.com.
> For more options, visit this group at
>
http://groups.google.com/group/protobuf?hl=en.