Java 客户端服务器程序无限输入

Java Client Server Program Takes input infinitely

我是 java 套接字编程的新手,我正在制作客户端服务器程序。服务器是多线程的。 当客户端与服务器的连接打开时。服务器像这样向客户端发送一个文本块:

connection is open with the server....
Welcome Please Chose one of the following Operations
Insert, Read, Update, Delete
Type Exit to terminate connection.

当我键入 read 或 exit 或任何操作时,它工作正常并且服务器响应。 但是当我选择一个操作即插入时出现问题 - >当服务器响应并要求我输入并且我想插入一个值时,程序不断地无限地接受输入我不知道问题出在哪里以及它是如何出现的发生。 这是相同的代码,客户端在选择操作时将输入作为一行发送,但是当我选择插入操作并且服务器期望一个值时,它会将其视为无限无尽的行。

客户class

public class Client1 {

public static void main(String[] args) throws IOException {

    Socket socket=null;

    try {

        System.out.println("sending connection request to host 127.0.0.1 at port 2000");
        socket = new Socket("127.0.0.1", 2000);

        System.out.println("connection is open with the server....");

        Scanner scn = new Scanner(System.in);

        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        DataInputStream dis = new DataInputStream(socket.getInputStream());

        while (true) {

            System.out.println(dis.readUTF());
            String tosend = scn.nextLine();
            dos.writeUTF(tosend);

            // If client sends exit,close this connection
            // and then break from the while loop
            if (tosend.equals("Exit")) {
                System.out.println("Closing this connection : " + socket);
                socket.close();
                System.out.println("Connection closed");
                break;
            }

            String received = dis.readUTF();
            System.out.println(received);
        }
        // closing resources
        scn.close();
        dis.close();
        dos.close();
    }

        catch (Exception e ){
        System.out.println(e);
    } finally {

        try {
            if (socket != null) socket.close();
        } catch (Exception e){
            System.out.println(e);
        }
    }
}

服务器Class

public class ServerThread extends Thread{
Socket socket ;
DataInputStream dis;
DataOutputStream dos;

ServerThread(Socket socket,DataInputStream dis,DataOutputStream dos ){
    this.socket = socket;
    this.dis=dis;
    this.dos=dos;
}

@Override
public void run(){

    String received;
    String toreturn;
    String welcomeText = """
            Welcome Please Chose one of the following Operations
            Insert, Read, Update, Delete
            Type Exit to terminate connection.""";

    while (true){

    try {
        // Ask user what he wants
        dos.writeUTF(welcomeText);


        // receive the answer from client
        received = dis.readUTF();


        if(received.equals("Exit"))
        {
            System.out.println("Client " + this.socket + " sends exit...");
            System.out.println("Closing this connection.");
            this.socket.close();
            System.out.println("Connection closed");
            break;
        }

        // write on output stream based on the
        // answer from the client
        switch (received) {
// the problem starts here if I chose insert and wanna print what the user typed, it takes  
//input infinitely from the user
            case "Insert":
                toreturn = "Inserting new info...";
                dos.writeUTF(toreturn);
                String out = dis.readUTF();
                dos.writeUTF("Accepted");
                dos.writeUTF(out);
                break;

            case "Read":
                toreturn = "Reading User Info...";
                dos.writeUTF(toreturn);
                break;

            case "Update":
                toreturn = "Updating User Info...";
                dos.writeUTF(toreturn);
                break;

            case "Delete":
                toreturn = "Deleting User Info";
                dos.writeUTF(toreturn);
                break;


            default:
                dos.writeUTF("Unknown User");

                break;
        }

    } catch ( IOException e) {
        e.printStackTrace();
    }
    }

    try
    {
        // closing resources
        this.dis.close();
        this.dos.close();

    }catch(IOException e){
        e.printStackTrace();
    }
    }
}

I don't know how this loop happens especially since the server accepts input correctly when choosing operations but when insert operation is chosen it just takes input infinitely, can anyone help please, i cant implement any operation if this problem persists .

我认为这是你的客户端,在 while 循环中尝试从服务器中删除第二次读取,这是因为你曾经读取过服务器发送的所有内容,当循环再次开始时想从服务器读取但没有任何东西可以读取读取并变为空闲。