Re: [protobuf] fails to parse from string

2010-11-10 Thread Brad Lira
client side:

address_book.SerializeToString(mystr)
strncpy(buf, mystr.c_str(), strlen(mystr.c_str()));
sendto(socket, buf, )

server side:

recvfrom(socket, buf, )
mystr.assign(buf, strlen(buf));

if (address_book.ParseFromString(mystr) == false)
{
   print deserialization failed
}

I get deserialization failed all the time, but get the message correctly though.

Maybe this method is not the right way to send string across socket.
I tried using SerializeToFileDescriptor(socket), that worked on the
client side, but on the server side, i never get the message with UDP sockets.
is there a better way of sending data across network?

thanks,


On Tue, Nov 9, 2010 at 5:43 PM, Kenton Varda ken...@google.com wrote:
 It sounds like you probably have extra bytes at the end of mystr which are
 not part of the protobuf.  The parser parses everything before those bytes
 just fine, but then chokes when it gets to the bytes it doesn't recognize.
  Please make sure you only pass in the exact bytes which came out of the
 serializer at the other end, no more, no less.

 On Tue, Nov 9, 2010 at 1:11 PM, Brad Lira snmp.apa...@gmail.com wrote:

 I have a c++ client/server application that sends messages using
 protocol buffers, however, at the server side, when i call

 address_book.ParseFromString(mystr)

 it returns false, but it actually gets the message correctly from
 client side, so i am not sure why it thinks that parsing has failed.
 any ideas?

 thanks,
 brad

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




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



Re: [protobuf] fails to parse from string

2010-11-10 Thread Evan Jones

Brad Lira wrote:

address_book.SerializeToString(mystr)
strncpy(buf, mystr.c_str(), strlen(mystr.c_str()));


strlen will return a shorter length than the real length, due to null 
characters. Use mystr.size()





Maybe this method is not the right way to send string across socket.
I tried using SerializeToFileDescriptor(socket), that worked on the
client side, but on the server side, i never get the message with UDP sockets.
is there a better way of sending data across network?


You probably want to use TCP sockets, since it provides retransmissions 
for you. Also, you'll need to prepend a length. See:


http://code.google.com/apis/protocolbuffers/docs/techniques.html#streaming


Or search the group archives for threads such as:

http://groups.google.com/group/protobuf/browse_thread/thread/3af587ab16132a3f


Good luck,

Evan

--
Evan Jones
http://evanjones.ca/

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



Re: [protobuf] fails to parse from string

2010-11-10 Thread Brad Lira
thanks,

yes it was the null character, on the server side when copying buffer
into string, i had add 1 to the
size of the buffer (i guess for the null), then the parsing was ok
with no error.



On Wed, Nov 10, 2010 at 1:42 PM, Evan Jones ev...@mit.edu wrote:
 Brad Lira wrote:

 address_book.SerializeToString(mystr)
 strncpy(buf, mystr.c_str(), strlen(mystr.c_str()));

 strlen will return a shorter length than the real length, due to null
 characters. Use mystr.size()



 Maybe this method is not the right way to send string across socket.
 I tried using SerializeToFileDescriptor(socket), that worked on the
 client side, but on the server side, i never get the message with UDP
 sockets.
 is there a better way of sending data across network?

 You probably want to use TCP sockets, since it provides retransmissions for
 you. Also, you'll need to prepend a length. See:

 http://code.google.com/apis/protocolbuffers/docs/techniques.html#streaming


 Or search the group archives for threads such as:

 http://groups.google.com/group/protobuf/browse_thread/thread/3af587ab16132a3f


 Good luck,

 Evan

 --
 Evan Jones
 http://evanjones.ca/


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



Re: [protobuf] fails to parse from string

2010-11-10 Thread Henner Zeller
On Wed, Nov 10, 2010 at 08:23, Brad Lira snmp.apa...@gmail.com wrote:
 client side:

 address_book.SerializeToString(mystr)
 strncpy(buf, mystr.c_str(), strlen(mystr.c_str()));

This is an error - you only copy to the first \0 byte (strlen looks
for a nul-terminated string) -- however, the string contains binary
data.

And: why do you copy the data in the first place to some buffer ? This
is an additional (potentially expensive) memory copy; why not use the
buffer directly from the string ?

sendto(socket, mystr.data(), mystr.size() ...)

 sendto(socket, buf, )

 server side:

 recvfrom(socket, buf, )
 mystr.assign(buf, strlen(buf));

 if (address_book.ParseFromString(mystr) == false)
 {
       print deserialization failed
 }

 I get deserialization failed all the time, but get the message correctly 
 though.

 Maybe this method is not the right way to send string across socket.
 I tried using SerializeToFileDescriptor(socket), that worked on the
 client side, but on the server side, i never get the message with UDP sockets.
 is there a better way of sending data across network?

 thanks,


 On Tue, Nov 9, 2010 at 5:43 PM, Kenton Varda ken...@google.com wrote:
 It sounds like you probably have extra bytes at the end of mystr which are
 not part of the protobuf.  The parser parses everything before those bytes
 just fine, but then chokes when it gets to the bytes it doesn't recognize.
  Please make sure you only pass in the exact bytes which came out of the
 serializer at the other end, no more, no less.

 On Tue, Nov 9, 2010 at 1:11 PM, Brad Lira snmp.apa...@gmail.com wrote:

 I have a c++ client/server application that sends messages using
 protocol buffers, however, at the server side, when i call

 address_book.ParseFromString(mystr)

 it returns false, but it actually gets the message correctly from
 client side, so i am not sure why it thinks that parsing has failed.
 any ideas?

 thanks,
 brad

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




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



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



Re: [protobuf] fails to parse from string

2010-11-10 Thread Evan Jones

On Nov 10, 2010, at 14:13 , Brad Lira wrote:

yes it was the null character, on the server side when copying buffer
into string, i had add 1 to the
size of the buffer (i guess for the null), then the parsing was ok
with no error.


Just adding 1 is still probably not correct. You have similar  
incorrect code on the receive side:



recvfrom(socket, buf, )
mystr.assign(buf, strlen(buf));



strlen(buf) is not going to give you the right thing. You should be  
using the return value from recvfrom(), which gives you the number of  
bytes that were read from the network.


Note: If you are using UDP, it will end up not working as soon as you  
have a message which is bigger than either your buffer, or the maximum  
UDP packet size, whichever comes first.


Evan

--
Evan Jones
http://evanjones.ca/

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



Re: [protobuf] fails to parse from string

2010-11-10 Thread Brad Lira
hmmm, i have to use UDP  in my case, TCP is not an option.

On Wed, Nov 10, 2010 at 2:40 PM, Evan Jones ev...@mit.edu wrote:
 On Nov 10, 2010, at 14:13 , Brad Lira wrote:

 yes it was the null character, on the server side when copying buffer
 into string, i had add 1 to the
 size of the buffer (i guess for the null), then the parsing was ok
 with no error.

 Just adding 1 is still probably not correct. You have similar incorrect code
 on the receive side:

 recvfrom(socket, buf, )
 mystr.assign(buf, strlen(buf));


 strlen(buf) is not going to give you the right thing. You should be using
 the return value from recvfrom(), which gives you the number of bytes that
 were read from the network.

 Note: If you are using UDP, it will end up not working as soon as you have a
 message which is bigger than either your buffer, or the maximum UDP packet
 size, whichever comes first.

 Evan

 --
 Evan Jones
 http://evanjones.ca/



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



Re: [protobuf] fails to parse from string

2010-11-09 Thread Kenton Varda
It sounds like you probably have extra bytes at the end of mystr which are
not part of the protobuf.  The parser parses everything before those bytes
just fine, but then chokes when it gets to the bytes it doesn't recognize.
 Please make sure you only pass in the exact bytes which came out of the
serializer at the other end, no more, no less.

On Tue, Nov 9, 2010 at 1:11 PM, Brad Lira snmp.apa...@gmail.com wrote:

 I have a c++ client/server application that sends messages using
 protocol buffers, however, at the server side, when i call

 address_book.ParseFromString(mystr)

 it returns false, but it actually gets the message correctly from
 client side, so i am not sure why it thinks that parsing has failed.
 any ideas?

 thanks,
 brad

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



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