Search code examples
javasocketsudpdatagram

What's the diffrence between DatagramPacket.getData().length and DatagramPacket.getLength() in java


In the code segment below,

DatagramPacket rPacket
rPacket  = new DatagramPacket(new byte[2000], 2000);
.. do some socket.receive ..

what would be the difference between DatagramPacket.getData().length and DatagramPacket.getLength() in java


Solution

  • The difference is that the first returns the size of the array used to construct the object, which never changes; the second returns the smaller of the length supplied to the constructor and the actual length of the smallest datagram most recently received, which changes on every receive.