[ee122] UDP's recvfrom() abstraction
fowler at eecs.berkeley.edu
Wed Nov 21 09:25:17 PST 2007
I think you're getting a little confused in layering... Let me try to
explain a possible approach and let me know if this is not what you're
>From a slide from the "Moves Institute":
Many OS's have memory limits on their input buffers that limit default
incoming UDP packets to 8192 bytes. Any host must take at least 512
bytes. You can fiddle with this to some extent. As a general rule,
don't go over 1500 bytes, because that's a popular ethernet MTU. Most
will be well below that. Moral: try not to fragment UDP packets
So what does this mean to you? It means that your MSS value that you
picked before will be pretty important. You want to stay within a
packet-size, which will be somewhere between 512B and 1500B.
Code-wise, you will have to control how much you send in one given
sendto(). Set that value (the 3rd index -- size_t len) to be your
MSS. On your receiving end, set your receive length to be your MSS
(again, the 3rd index -- size_t len).
Your *app* will (since you want to create the stream abstraction)
"just" call my_send(). Within my_send(), you should have a loop that
breaks the larger application data unit or application message into
MSS-sized packets and sends those individually. Each of those packets
should have the custom header. (each call to sendto() generates a
single UDP packet)
On the other side, my_recv() would do a similar loop, reconstructing
the application message from those MSS-sized packets.
Let me know if that doesn't answer your question.
On Nov 21, 2007 4:15 AM, Jeremy Fleischman
<jeremyfleischman at berkeley.edu> wrote:
> 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
> Jeremy Fleischman
> ee122 mailing list
> ee122 at mailman.ICSI.Berkeley.EDU
More information about the ee122