简体   繁体   English

在 Java 和 C++ 应用程序中使用 protobuf 进行解析时出错

[英]Error during parsing with protobuf in Java and C++ Application

I try to use protobuf for message exchange between two java applications (client and server).我尝试使用 protobuf 在两个 Java 应用程序(客户端和服务器)之间进行消息交换。 The protobuf code is completely developed in C++ (the TLS client/server logic too). protobuf 代码完全用 C++ 开发(TLS 客户端/服务器逻辑也是如此)。 The 'raw' protobuf classes are wrapped by the C++ code in a simple C++ class which is just delegating the method calls to the original protobuf code. “原始”protobuf 类由 C++ 代码包装在一个简单的 C++ 类中,该类只是将方法调用委托给原始 protobuf 代码。 This C++ Wrapper is again wrapped for Java with SWIG.*此 C++ 包装器再次使用 SWIG 为 Java 包装。*

I fill the protobuf in the java client with the following calls:我使用以下调用填充 java 客户端中的 protobuf:

Message msg = new Message();
msg.setUser("test");
msg.setPassword("pwd");

which will delegate the information to the C++ Wrapper and from there to protbuf.它将信息委托给 C++ 包装器,然后从那里委托给 protbuf。 The data is transmitted correctly, but if I try to parse the serialized data on the java server side the parsing fails:数据传输正确,但如果我尝试在 java 服务器端解析序列化数据,解析失败:

Message message = new Message();
boolean p = message.parse(data); // will call ParseFromString(data); on C++  layer with try catch

However on the c++ layer of the server application it works fine.但是在服务器应用程序的 c++ 层上它工作正常。

I compared the 'raw' protobuf data on C++ and Java layer (server side) and noticed that they look different:我比较了 C++ 和 Java 层(服务器端)上的“原始”protobuf 数据,发现它们看起来不同:

//c++
test�pwd
//java
testÿpwd

The length information is not shown on stackoverflow, but they are the same on java and c++.长度信息在stackoverflow上没有显示,但是在java和c++上是一样的。

What could cause this difference?什么可能导致这种差异? Is there some problem with the encoding?编码有问题吗?

(*) I decided to go this extra step because in this case all the protobuf logic is hiden for SWIG. (*) 我决定执行这额外的步骤,因为在这种情况下,所有 protobuf 逻辑都为 SWIG 隐藏。

Note: I only work with strings and use parseFromString and SerializeAsString from C++.注意:我只使用字符串并使用 C++ 中的parseFromStringSerializeAsString Java does not call this methods directly. Java 不直接调用此方法。

EDIT: Message has two more fields 'bool success' and 'string name'.编辑:消息还有两个字段“布尔成功”和“字符串名称”。 If I remove them both, the parsing works (the symbol between user and pwd was gone).如果我将它们都删除,则解析工作(user 和 pwd 之间的符号消失了)。 Why?为什么?

message Message{  
    required string user = 1;
    optional string pwd = 2;
    //optional string name = 3; 
    //optional bool success = 4;    
}

EDIT It was indeed a problem with Java String encoding.编辑这确实是 Java 字符串编码的问题。 Also see my other related question here on Stackoverflow另请参阅我在 Stackoverflow 上的其他相关问题

looking at the protobuf java api, it always uses "byte[]" for serialized data.查看 protobuf java api,它总是使用“byte[]”作为序列化数据。 i suspect you are having problems because java Strings are not the same as c++ strings.我怀疑您遇到了问题,因为 java 字符串与 c++ 字符串不同。 I would use byte[] exclusively for the data in Java.我会专门将 byte[] 用于 Java 中的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM