Hi,
I am trying to figure out how to use protobuf for socket communication
between client and server (both in c++).
I notice the serialize and parse method are either using string for a
ostream/istream.
And in c++ std, I don't think there is a way to create a
ostream/istream via socket.
So I am thinking about serialize it to a std string, get a c string
out of that, and then write the c string to the socket. For the receiving end,
it read to a buf, build a std string, and parse it using protobuf.
So I tried the following:
MyMessage msg1;
msg1.set_type(ipc::MyMessage::TYPE1);
string msgStr;
msg1.SerializeToString(&msgStr);
// put the serialized content to a buffer so that I can write it to socket
char buf[128];
memset((char *) buf, 0, 128);
memcpy (buf, msgStr.c_str(), requestMessage.ByteSize());
// pretend the receiving end get the serialized content from
// socket and try to reconstruct the message
MyMessage msg2;
if (msg2.ParseFromString(string(buf))) {
cout << "True" << endl;
} else {
// alway return false here
cout << "false" << endl;
}
But I always get 'false' when I reconstruct the message from a buffer.
Thank you.
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en.