【问题标题】:Java Client Server Program Takes input infinitelyJava 客户端服务器程序无限地接受输入
【发布时间】:2021-09-05 07:05:29
【问题描述】:

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

客户端类

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

服务器类

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

我不知道如何发生这种循环,因为服务器在选择操作时正确接受输入,但选择了插入操作时,它只是在无限制地取消输入,如果此问题仍然存在,我无法实现任何操作。

【问题讨论】:

    标签: java sockets server client client-server


    【解决方案1】:

    我认为它是您的客户端,在 while 循环中尝试从服务器中删除第二次读取,这是因为您曾经读取了服务器发送的所有内容,并且当循环再次开始时想要从服务器读取但没有什么可读取的变得空闲。

    【讨论】:

    • 你是对的 非常感谢,我没注意那部分。
    猜你喜欢
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多