> How come I have to specify the size of the input buffer when
> receiving UDP DatagramPackets? I basically want to receive as many
> bytes as the sent datagram contains?
You have to provide a buffer large enough for the largest expected
packet, and you have to tell the DatagramPacket how much of the buffer
it can fill. I suppose it could use buffer.length() itself, but there
may be cases when you don't want to fill the entire buffer.
After receiving a datagram, the length is updated to tell you how many
bytes were actually copied into the buffer.
It is extremely unfortunate that the same "length" is used for both
purposes, since it is far from obvious that you need to reset the
value before reusing the same DatagramPacket to receive additional
packets (otherwise you will receive successively smaller and smaller
packets, regardless of what is actually sent).
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Chris Uppal - 17 Nov 2005 09:47 GMT
> (otherwise you will receive successively smaller and smaller
> packets, regardless of what is actually sent).
Oooh, that's funny!
(Didn't quite ROFL, but did LOL)
-- chris
Gordon Beaton - 17 Nov 2005 09:09 GMT
> Oooh, that's funny!
>
> (Didn't quite ROFL, but did LOL)
I remember swearing out loud when I discovered this cause of a UDP
communication problem.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Chris Uppal - 17 Nov 2005 11:36 GMT
[me:]
> > (Didn't quite ROFL, but did LOL)
>
> I remember swearing out loud when I discovered this cause of a UDP
> communication problem.
As with all coarse physical humour, it's not funny if /everyone/ laughs...
-- chris
Roedy Green - 17 Nov 2005 12:27 GMT
>I remember swearing out loud when I discovered this cause of a UDP
>communication problem.
I documented it as a gotcha at http://mindprod.com/jgloss/udp.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
On Thu, 17 Nov 2005 09:49:51 +0100, Andersen
<andersen_800@hotmail.com> wrote, quoted or indirectly quoted someone
who said :
>How come I have to specify the size of the input buffer when receiving
>UDP DatagramPackets? I basically want to receive as many bytes as the
>sent datagram contains?
the whole idea of a buffer is to have memory ready to go to collect
the incoming characters. If your buffer is too small, then what is
supposed to happen to the characters coming in while you are
frantically allocating more space?
In a socket you have enough buffers pre-allocated so that you can
effectively send a "pause" message to the other end before you
overflow. Even if you overflow that is not the end of the world. In
TCP/IP can request a retransmission.
With UDP if you don't get the packet the first time, it is gone. That
is why you need a buffer ready to take all the bits without a hitch.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.