0

C++ serialization:

int main ()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    proto::Request request;
    std::string output;

    request.set_fieldone("X");
    request.set_fieldtwo("Y");

    //  Prepare our context and socket
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REQ);
    socket.connect ("tcp://localhost:5555");

    request.SerializeToString(&output);

    long size = output.length();
    zmq::message_t request(size);
    memcpy(request.data(), &output, size);

    socket.send(request);

    return 0;

}

Python de-serialization:

def __init__(self):
    self.database.connect()
    self.context = zmq.Context()
    self.socket = self.context.socket(zmq.REP)
    self.socket.bind("tcp://*:5555")
    self.request = call_init_pb2.DialRequest()

def run(self):
    message = self.socket.recv()
    self.request.ParseFromString(message)

Which gives me the error message:

self.request.ParseFromString(message)
google.protobuf.message.DecodeError: Error parsing message

What I would like to achieve is to serialize the message in C++, send it over a network to a Python server. De-serialize the message, and then perform some business logic on the server side to check for certain properties of the message. I can send the string, and its received with the same length and type at the Python server side, but the parsing doesn't work.

Did I miss some fundamental basic knowledge?

1 Answer 1

1

I solved this issue, using SerializeToArray rather than SerializeToString approach.

Here is the code I found from https://groups.google.com/forum/#!topic/protobuf/AmdloRxdFUg which solved this "issue"

 int size = request.ByteSize();
 char* array = new char[size];
 request.SerializeToArray(array, size);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.