In your java code you use a 1024 byte buffer to recieve udp packets, which will only be filled partially if you don't receive enough bytes. receivePacket.getData()
simply returns this buffer.
When you then convert it to a String, of course the remaining bytes will be filled with whatever was in the buffer before (in this case zero bytes, but could als be filled with content if you used it as receiving buffer before).
The correct way to get the received data would be to use the getLength()
and getOffset()
methods of DatagramPacket
:
strData = new String(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());