title: Why is this Java UDP packet length too long?
body: <p>So I have a java server and client, the data is being sent to the server fine and the server is interperating it, but i found that the client takes ages to respond to what the server has sent it, after some time looking around i found that the data my server is sending the client is way longer than the data that should be sent.</p>
  
  <p>The packet that is sent to the client has all the data that I sent however it also has a lot of white space after it, I would like to fix this, anyone have any ideas?</p>
  
  <p>My code to get the data is a simple for loop of every client on the server, this adds the client data to a string and that string is added to a packet:</p>
  
  <p>class PlayerList</p>
  
  <pre><code>public static String getString()
  {
      String message = "";
  
      for(int x = 0; x &lt; list.size(); x++)
      {
          Player player = list.get(x);
  
          if(message.equals(""))
          {
              message += player.name+";"+player.address+";"+player.pos[0]+";"+player.pos[1]+";"+player.fakeRotation+";"+player.rotation+";"+player.rotationSpeed+";"+player.speed+";"+player.sheildEnabled+";"+player.sheildStrength+";"+player.health;
          }
          else
          {
              message += ","+player.name+";"+player.address+";"+player.pos[0]+";"+player.pos[1]+";"+player.fakeRotation+";"+player.rotation+";"+player.rotationSpeed+";"+player.speed+";"+player.sheildEnabled+";"+player.sheildStrength+";"+player.health;
          }
      }
  
      System.out.println(message);
  
      return message;
  }
  </code></pre>
  
  <p>Class Send</p>
  
  <pre><code>while(Server.serverRunning)
      {
          for(int p = 0; p &lt; PlayerList.list.size(); p++)
          {
              Player player = PlayerList.list.get(p);
  
              try
              {
                  byte[] buf = PlayerList.getString().getBytes();
  
                  //send the message to the client to the given address and port
                  packet = new DatagramPacket(buf, buf.length, player.address);
                  Server.socket.send(packet);
              }
              catch (IOException e)
              {
                  System.out.println("Can't send packet to player: "+player.name);
              }
          }
      }
  </code></pre>
  
  <p>I know the data recieved from the getString method is correct and has no white space as I have tested it, so it must be happening when i add the string to the packet.</p>
  
  <p>The intended data shows up in the output as:
      <code>Luke;127.0.0.1:63090;50.0;50.0;0.0;0.0;0.0;0.0;true;100;100</code></p>
  
  <p>however the actual data shows up on the client as:
      <code>Luke;127.0.0.1:63090;50.0;50.0;0.0;0.0;0.0;0.0;true;100;100 (lots of spaces here) ...line is too long, please switch to wrapped mode to see whole line...</code></p>
  
  <p>The client code to receive the data is:</p>
  
  <pre><code>receiveData = new byte[clientSocket.getReceiveBufferSize()];
                  receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  clientSocket.receive(receivePacket);
                  receiveMessage = new String(receivePacket.getData());
  </code></pre>