title: How to get rid of the empty remaining of the buffer?
body: <p>I have a server-client application that is using a datagram socket to exchange messages. I have initially set the buffer size to be 1024 bytes because I dont know the length of the messages. When I send something that is shorter than 1024 bytes I get the rest of my string displayed as some weird characters (null characters or I am not sure how they are called).
  Here is a screen:
  <img src="https://i.stack.imgur.com/5t6WB.png" alt="string buffer null characters"></p>
  
  <p>Client code:
  <code>byte[] buf = ("This is another packet.\n").getBytes();
  DatagramPacket packet = new DatagramPacket(buf, buf.length, inetAddress, serverport);
  socket.send(packet)</code></p>
  
  <p>Server code:
  <code>byte[] buf = new byte[1024];
  DatagramPacket packet = new DatagramPacket(buf, buf.length);
  socket.receive(packet);</code></p>