> DatagramSocket ds;
> ds = new DatagramSocket(8081);
> DatagramPacket dpi;
> dpi = new DatagramPacket(new byte[512], 512);
> while(true) {
> ds.receive(dpi);
Comparing this with some working code of my own, the only difference I can see
is that I set the size of the DatagramPacket explicitly before each receive().
My code reads:
DatagramSocket socket = new DatagramSocket(PORT);
DatagramPacket packet = new DatagramPacket(
new byte[BUFFER_SIZE],
BUFFER_SIZE);
for (;;)
{
packet.setLength(BUFFER_SIZE);
socket.receive(packet);
...
The Java network stuff treats the length of the packet as separate from the
length of the buffer which is used to send/receive it.
-- chris
Gordon Beaton - 03 Oct 2006 13:55 GMT
> The Java network stuff treats the length of the packet as separate
> from the length of the buffer which is used to send/receive it.
Unfortunately the length associated with the DatagramPacket is used
for two purposes: to tell receive() how much you want to receive, and
to find out how much was actually received.
So if you attempt to reuse a DatagramPacket but forget to call
setLength() before receive(), you will find that your receives can
only get shorter as a result, never longer, until you reach 0.
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e