[Solved] When IDEA is conducting UDP half-duplex communication, the client input is normal, and the server accepts the solution that prints out garbled boxes.

During UDP half-duplex communication, the data received by the server appears as client data followed by a large series of block codes, as shown in the figure
Client input works fine:
Please add image description
The server accepts a box with garbled characters

Please add image description
Client code:

package javaweb2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class UdpClient {<!-- -->
    public static void main(String[] args) throws IOException {<!-- -->
        // client sends data to server
        DatagramSocket ds = new DatagramSocket();
        while (true) {<!-- -->
            System.out.println("Client: Please enter the sent data");
            Scanner scanner = new Scanner(System.in);
            String context = scanner.nextLine();
            if ("666".equals(context)) {<!-- -->
                break;
            }
            byte[] bytes = context.getBytes();
            //create socket connection
            ds = new DatagramSocket();
            // encapsulate the sent packet
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("localhost"), 8080);
            // start sending data
            ds.send(dp);
            System.out.println("Data sent successfully");
        }
        //close the resource
        ds.close();

    }
}
piece

Server code:

package javaweb2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpServer {<!-- -->
    public static void main(String[] args) throws IOException {<!-- -->
        //The server always accepts the data sent by the client
        //create socket object
        DatagramSocket ds =new DatagramSocket(8080);
        System.out.println("Waiting to accept the data sent by the client...");
        while(true){<!-- -->
            //create receiver packet
            byte[] bytes=new byte[1024];
            DatagramPacket dp=new DatagramPacket(bytes, bytes.length);

            ds.receive(dp);
            System.out.println("The server received the data sent by the client");
            System.out.println(new String(dp.getData()).trim());//You need to add trim() after the output to remove the blanks at the beginning and end of the string, so that the garbled characters in the printed box can be removed.
        }
       // ds.close();
    }
}

When accepting the data sent by the client and needing to print the data on the client, you can add .trim() after the output string to remove the blanks at the beginning and end of the string, and the box can be eliminated gibberish.
After adding .trim as shown in the figure, the data of the client can be printed normally on the server.
Please add image description
Guys, go and try it!