In the following code for sockets, I declare a byte buffer and listen for client messages in a loop:
// Create some buffer
byte[] buffer = new byte[512];
while (true)
{
// Listen for messages from Client
int length = socket.getInputStream().read(buffer);
if (length != -1)
System.out.println(new String(buffer));
else
break;
// Reset buffer here to zeros?
}
Each time I'd require to reset the contents of buffer to receive a new message. What's the proper way to do this?
Agree with dasblinkenlight answers, but to answer the question itself (how to reset an array), you can use Arrays.fill
to set a value to every cell
byte[] array = new byte[10];
Arrays.fill(array, (byte)0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With