在下面的代码段中,
DatagramPacket rPacket
rPacket = new DatagramPacket(new byte[2000], 2000);
.. do some socket.receive ..
java中DatagramPacket.getData().length和DatagramPacket.getLength()有什么区别
不同的是,第一个返回用于构造对象的数组的大小,它永远不会改变;第二个返回提供给构造函数的长度和最近接收的最小数据报的实际长度中较小的一个,每次接收都会改变。
来自 DataGrammPacket 类的 javadoc
获取长度():Returns the length of the data to be sent or the length of the data
received.
获取数据():Returns the data buffer. The data received or the data to be sent
starts from the <code>offset</code> in the buffer,
and runs for <code>length</code> long.
此外,您还必须知道 setData():
Set the data buffer for this packet. This sets the
* data, length and offset of the packet.
*
* @param buf the buffer to set for this packet
*
* @param offset the offset into the data
*
* @param length the length of the data
* and/or the length of the buffer used to receive data
setData(byte[] buf, int offset, int length)
构造函数还调用 setData()