UDP comm

G

Gordon Beaton

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
 
A

Andersen

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?

andersen
 
G

Gordon Beaton

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
 
C

Chris Uppal

Gordon said:
(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
 
R

Roedy Green

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.
 
C

Chris Uppal

Gordon Beaton wrote:

[me:]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top