[ee122] UDP's recvfrom() abstraction

Jeremy Fleischman jeremyfleischman at berkeley.edu
Wed Nov 21 04:15:56 PST 2007


TCP provides us with a guaranteed stream of data. This makes things
tricky for a TCP receiver in cases where we need to separate two
messages a receiver sends us. For example, if the server sends us the
message "hi" and then "how are you", all the receiver will eventually
get is "hi how are you". But it is possible that the sender was saying
hello to an old friend of his named "how", and was then going to ask
him "are you doing your ee122 project".
It is easier to see the two possibilities with punctuation.
Hi how are you?... OR Hi How. Are you doing your ee122 project?

This problem is dealt with using the computer science equivalent of
punctuation, delimiters. Remember good 'ol <CRLF>?

My understanding is that UDP does not abstract away the underlying
packets to be a stream of data, as it doesn't make much sense for a
stream to be arbitrarily cut and twisted upon itself. But Beej's guide
says that we use recvfrom() to receive data over UDP. The arguments to
recvfrom() are the same as recv() except for the addition of two
arguments specifying the sender to receive from. So my question is
this: if the sender sends us two distinct UDP packets like this:
"hi how", "are you doing your ee122 project"
and we call recvfrom() asking for something like 100 bytes, could we
get this: "hi how are you doing your ee122 project" or maybe "hi how"
or maybe just "hi"? It doesn't make sense to concatenate the two
packets ("hi how are you doing your ee122 project"), and it doesn't
make sense to receive part of a packet either ("hi") as packets either
make it entirely or are dropped.

This same question applies to the sender side as well. Will two calls
to UDP's sendto() always result in two UDP packets? sendto() returns
an int specifying the number of bytes sent. So, if I want to send the
message "hi how are you ", and I call sendto() with this buffer, but
sendto() returns 3, all I've done is send the message "hi " out on a
UDP packet. I would then call sento() again, and it may return 4,
which means that I sent "how " out on a packet. I finally call
sendto() one more time, and it returns 8, meaning I've sent out "are
you " (the rest of my message). But let's say that the packet "hi "
gets dropped, and the other 2 packets get reordered, which means that
the receiver could potentially receive the packet "are you " and then
"how ", which makes the message appear to be questioning if your name
is "how", as opposed to the salutations it was meant to be.

I hope I have been clear in my discussion of the problems I fear could
arise using UDP's sendto() and recvfrom(). I feel that the problem
that arises if sendto() sends only part of your message is impossible
to fix without lower level control where we construct the UDP packets
ourselves.

Jeremy Fleischman


More information about the ee122 mailing list