serialize message to UDP socket

2,224 views
Skip to first unread message

jayt...@gmail.com

unread,
Sep 18, 2009, 3:19:29 PM9/18/09
to Protocol Buffers
Hello all,

I am having trouble figuring out how to serialize data over a socket
utilizing UDP protocol. I am in C++ environment. When writing to the
socket without protocol buffers, I use the standard sendto() socket
call which allows me to specify the port and IP address of the
intended receiver of my UDP message. When trying to send a protocol
buffers message, this seems to be the recommended strategy on the
google docs:

ZeroCopyOutputStream* raw_output = new FileOutputStream
(sock);
CodedOutputStream* coded_output = new CodedOutputStream
(raw_output);
coded_output->WriteRaw(send_data,strlen(send_data));

There is no way to specify what the port and IP address is here,
analogous to when using the standard sendto() socket writing call. So
my message never gets received by the intended recipient on the
network. I am aware that this is a raw message, not a PB message.
Getting this raw message over the network is a first step in
accomplishing the ultimate goal of getting the PB message over the
network.

Is there a way to get all of the bytes of a serialized PB message into
raw form and then send them with sendto()?

Any ideas? Thanks for any help.

Jay

jayt...@gmail.com

unread,
Sep 18, 2009, 3:26:16 PM9/18/09
to Protocol Buffers
One other thing I wanted to say was that I chose to use
CodedOutputStream to send
data because ultimately I have to manually encode a length prefix in
front of my PB message.
With the C++ environment, I understand that this is the only way to do
this (ugh is right; I am sure this is a common problem with using PB
over sockets that remain in use).
I am fully aware that there are methods to serialize directly from the
object but those will not serve my ultimate aim of getting a length
prefix ahead of the data bytes.

Thanks

Jay

Kenton Varda

unread,
Sep 18, 2009, 4:03:21 PM9/18/09
to jayt...@gmail.com, Protocol Buffers
You want to serialize your message to an array first (use SerializeToString() or SerializeToArray()), then write that using send().  Don't use FileOutputStream -- it is meant for stream-based I/O, not UDP.

Christopher Smith

unread,
Sep 19, 2009, 8:09:26 PM9/19/09
to jayt...@gmail.com, Protocol Buffers
Doesn't the UDP packet header effectively provide that length prefix for you?

--Chris
--
Chris

Adarsh Singh

unread,
Jul 12, 2023, 2:36:52 AM7/12/23
to Protocol Buffers
I serialized my protobuff messages using the SerializedToArray() method. and send it to the server over an UDP socket. But On  receiver side when I tried to parse the data, I'm not able to parse the data from the UDP socket_fd. Can someone please  help me to get over this problem.  with TCP socket it's working fine. But I want to implement using UDP socket. 

thank you,
Adarsh Singh 

Adam Cozzette

unread,
Jul 12, 2023, 2:39:18 PM7/12/23
to Adarsh Singh, Protocol Buffers
I think in this case it would help to first make sure that you're able to successfully get the raw bytes from one side to the other over UDP (i.e. log the byte arrays on the sending side and receiving side and make sure they are the same length and are identical). This seems like the part that is most likely to be the problem. Once you're able to get the raw bytes across successfully, parsing the proto should be fairly easy.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/8be9bc35-3c3f-455f-9390-ba2de96aaa9dn%40googlegroups.com.
Message has been deleted

Adarsh Singh

unread,
Jul 17, 2023, 12:36:44 AM7/17/23
to Protocol Buffers
Hi all, 
Do anyone have idea about how we can receive the proto buffer serialized message on the receiver side via a udp socket. I am not receiving the data while using the recvfrom and any other method as well. if any one has an example please share it. 

thank you,
adarsh singh

On Saturday, 19 September 2009 at 00:49:29 UTC+5:30 jayt...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages