在下面的代码段中,
DatagramPacket rPacket
rPacket = new DatagramPacket(new byte[2000], 2000);
.. do some socket.receive ..
在java中DatagramPacket.getData()。length和DatagramPacket.getLength()之间有什么区别
答案 0 :(得分:2)
不同之处在于第一个返回用于构造对象的数组的大小,该数组永远不会改变;第二个返回提供给构造函数的较小长度和最近收到的最小数据报的实际长度,这在每次接收时都会发生变化。
答案 1 :(得分:0)
来自DataGrammPacket类的javadoc
getLength():Returns the length of the data to be sent or the length of the data
received.
getData():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()