为什么这个Java UDP数据包长度太长?

为什么这个Java UDP数据包长度太长?,java,udp,client,packet,Java,Udp,Client,Packet,所以我有一个java服务器和客户机,数据被发送到服务器,服务器正在交互,但是我发现客户机需要很长时间才能响应服务器发送的数据,环顾四周一段时间后,我发现我的服务器发送给客户机的数据比应该发送的数据长得多 发送到客户端的数据包包含了我发送的所有数据,但是它后面也有很多空白,我想解决这个问题,有人有什么想法吗 我获取数据的代码是服务器上每个客户机的一个简单for循环,它将客户机数据添加到字符串中,并将该字符串添加到数据包中: 班级演奏家 public static String getString(

所以我有一个java服务器和客户机,数据被发送到服务器,服务器正在交互,但是我发现客户机需要很长时间才能响应服务器发送的数据,环顾四周一段时间后,我发现我的服务器发送给客户机的数据比应该发送的数据长得多

发送到客户端的数据包包含了我发送的所有数据,但是它后面也有很多空白,我想解决这个问题,有人有什么想法吗

我获取数据的代码是服务器上每个客户机的一个简单for循环,它将客户机数据添加到字符串中,并将该字符串添加到数据包中:

班级演奏家

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;
}

DatagramPacket上的getData返回整个缓冲区,该缓冲区的末尾可能有额外的数据。您需要调用getLength()来确定接收数据的实际长度,并且只查看getData()中的字节


DatagramPacket上的getData返回整个缓冲区,该缓冲区的末尾可能有额外的数据。您需要调用getLength()来确定接收数据的实际长度,并且只查看getData()中的字节



我认为这是因为您试图创建buf.length大小的数据报,但您只发送player.address。因此,剩余的空间将被空格填充。您没有显示接收数据包的客户端代码。数据包的末尾有空格,或者它有\0秒?它可能会使用nulls.im将其填充到其默认数据包大小,并将buf发送到player.address,因为我的databuf长度为60,所以它应该发送fine。我认为这是因为您试图创建具有buf.length大小的数据报,但您只发送player.address。因此,剩余的空间将被空格填充。您没有显示接收数据包的客户端代码。数据包的末尾有空格,或者它有\0秒?它可能会使用nulls.im将其填充到默认数据包大小,并使用buf发送到player.address,因为我的databuf长度为60,所以它应该发送fineSo以解决发送数据时的数据长度问题,我可以将其更改为byte[]realData=Arrays.copyOf(PlayerList.getString().getBytes(),PlayerList.getString().getBytes().length);不,你的发送很好,问题在于接收。嗯,我不明白为什么服务器发送数据要花这么长时间,当我按下客户端上的一个键时,服务器会立即接收数据,并立即对数据进行翻译,但是这些数据需要一段时间才能显示在客户机上。我的回答是关于你获取客户机上的无关数据。我需要查看更多的代码(客户端和服务器),以查看是否存在会导致延迟的错误。您能否通过客户端和服务器之间的ping来验证两个方向的延迟是否相同?如果延迟明显不同,则可能是网络问题,而不是代码问题。我可以确认,从客户端发送到服务器的数据比从服务器发送到客户端的数据快得多。因此,为了解决发送数据时的数据长度问题,我可以将其更改为字节[]realData=Arrays.copyOf(PlayerList.getString().getBytes(),PlayerList.getString().getBytes().length);不,你的发送很好,问题在于接收。嗯,我不明白为什么服务器发送数据要花这么长时间,当我按下客户端上的一个键时,服务器会立即接收数据,并立即对数据进行翻译,但是这些数据需要一段时间才能显示在客户机上。我的回答是关于你获取客户机上的无关数据。我需要查看更多的代码(客户端和服务器),以查看是否存在会导致延迟的错误。您能否通过客户端和服务器之间的ping来验证两个方向的延迟是否相同?如果延迟明显不同,则可能是网络问题,而不是代码问题。我可以确认,从客户端发送到服务器的数据比从服务器发送到客户端的数据快得多。
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);
            }
        }
    }
receiveData = new byte[clientSocket.getReceiveBufferSize()];
                receivePacket = new DatagramPacket(receiveData, receiveData.length);
                clientSocket.receive(receivePacket);
                receiveMessage = new String(receivePacket.getData());
byte[] realData = Arrays.copyOf( receivePacket.getData(), receivePacket.getLength() );