Search code examples
javaudpclientpacket

Why is this Java UDP packet length too long?


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.

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?

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:

class PlayerList

public static String getString()
{
    String message = "";

    for(int x = 0; x < 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;
}

Class Send

while(Server.serverRunning)
    {
        for(int p = 0; p < 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);
            }
        }
    }

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.

The intended data shows up in the output as: Luke;127.0.0.1:63090;50.0;50.0;0.0;0.0;0.0;0.0;true;100;100

however the actual data shows up on the client as: 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...

The client code to receive the data is:

receiveData = new byte[clientSocket.getReceiveBufferSize()];
                receivePacket = new DatagramPacket(receiveData, receiveData.length);
                clientSocket.receive(receivePacket);
                receiveMessage = new String(receivePacket.getData());

Solution

  • getData on DatagramPacket returns the entire buffer, which may have extra data at the end. You need to call getLength() to determine the actual length of data received, and only look at those bytes from getData()

    byte[] realData = Arrays.copyOf( receivePacket.getData(), receivePacket.getLength() );