我有一条 protobuf 消息,在解析时会崩溃。
我在同一台机器上运行客户端和服务器并序列化字节流。
我使用 java 中的 Arrays.equal 将其与接收到的字节数组进行比较。 它说它们是平等的。
我的 protobuf 只有一个固定的 32 位字段存储 IP 地址。
我使用了 google protobuf 2.4.1 和 2.5.0 版本,但没有成功。我在这里缺少什么吗?
堆栈跟踪:
com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).
at com.google.protobuf.InvalidProtocolBufferException.invalidTag(InvalidProtocolBufferException.java:89)
at com.google.protobuf.CodedInputStream.readTag(CodedInputStream.java:108)
... ...
Protobuf代码:
message IpMessage {
required fixed32 ip = 1;
}
编译:
protoc --java_out=. ip-message.proto
为了解析消息,我使用:
IpMessage.parseFrom(receivedPkt); //receivedPkt is byte[]
如果您的代码在当前二进制文件位置解析错误的 protobuf 消息,则会出现此错误。在上面的例子中,当前的
receivedPkt
不是 IpMessage
。