简体   繁体   中英

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). The protobuf code is completely developed in C++ (the TLS client/server logic too). 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. This C++ Wrapper is again wrapped for Java with SWIG.*

I fill the protobuf in the java client with the following calls:

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

which will delegate the information to the C++ Wrapper and from there to protbuf. The data is transmitted correctly, but if I try to parse the serialized data on the java server side the parsing fails:

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.

I compared the 'raw' protobuf data on C++ and Java layer (server side) and noticed that they look different:

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

The length information is not shown on stackoverflow, but they are the same on java and 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.

Note: I only work with strings and use parseFromString and SerializeAsString from C++. Java does not call this methods directly.

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). 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. Also see my other related question here on Stackoverflow

looking at the protobuf java api, it always uses "byte[]" for serialized data. i suspect you are having problems because java Strings are not the same as c++ strings. I would use byte[] exclusively for the data in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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