enum deserialization

180 views
Skip to first unread message

Enrico Viappiani

unread,
Jul 10, 2011, 10:07:44 AM7/10/11
to Protocol Buffers
I've got a java server and a c++ client, client sends a message to the
server.

In a first moment we had the message structure made by simple format
like strings or integers, and the message sending/receiving with
serialization and deserialization were working perfectly.

Nowadays we added a "enum" in the structure and now on the java side,
the deserialization doesn't work anymore:

c++ client sending:

bool Client::SendMsg(const char* pStr)
{
if (send(m_iSock, (char *) pStr, strlen(pStr), 0) == -1) //
send(id_socket,stringa,lunghezza stringa,flags)
{
perror("Client::SendString, send");
return false;
}
return true
}

//on the client'smain
ss=(const char*)malloc(1000);
ss = obj.c_str();
mylink.SendMsg(ss);

java server receiving:

public int ReceiveMsg() throws IOException
{

InputStream InMsg = sock.getInputStream();
ProtoMessage Msg = ProtoMessage.parseFrom(InMsg); //THIS
THROWS THE ERROR
sock.close;
}


the error is:
Exception in thread "main"
com.google.protobuf.InvalidProtocolBufferException: While parsing a
protocol message, the input ended unexpectedly in the middle of a
field. This could mean either than the input has been truncated or
that an embedded message misreported its own length.
at
com.google.protobuf.InvalidProtocolBufferException.truncatedMessage(InvalidProtocolBufferException.java:
49)
at
com.google.protobuf.CodedInputStream.refillBuffer(CodedInputStream.java:
711)
at
com.google.protobuf.CodedInputStream.readRawByte(CodedInputStream.java:
734)
at
com.google.protobuf.CodedInputStream.readRawVarint32(CodedInputStream.java:
338)
at
com.google.protobuf.CodedInputStream.readInt32(CodedInputStream.java:
198)


On the other side otherwise c++ client can deserialize the stream
perfectly.
I'm gettin mad, without the enum the send/receive is working!
any ideas?
thanks in advance
Enrico

Jason Hsueh

unread,
Jul 11, 2011, 1:10:46 PM7/11/11
to Enrico Viappiani, Protocol Buffers
On Sun, Jul 10, 2011 at 7:07 AM, Enrico Viappiani <viap...@gmail.com> wrote:
I've got a java server and a c++ client, client sends a message to the
server.

In a first moment we had the message structure made by simple format
like strings or integers, and the message sending/receiving with
serialization and deserialization were working perfectly.

Nowadays we added a "enum" in the structure and now on the java side,
the deserialization doesn't work anymore:

c++ client sending:

bool Client::SendMsg(const char* pStr)
{
       if (send(m_iSock, (char *) pStr, strlen(pStr), 0) == -1)        //
send(id_socket,stringa,lunghezza stringa,flags)

You should not use strlen on protobuf encoded data. You're probably serializing the enum value with integer value 0, which gets encoded as the null char on the wire. As a result, your C++ client is not sending any data after the enum tag.
 
       {

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Reply all
Reply to author
Forward
0 new messages