简体   繁体   中英

Java Client Server Program Takes input infinitely

I'm new to java socket programming, I'm making a client server program. the server is multithreaded. when the client connection with server is open. the server sends a text block to the client like this:

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

when I type read or exit or any of the operations it works fine and the server responds. but the problem happens when I chose an operation ie insert --> when the server responds and asks me for input and I wanna insert a value, the program keeps taking input infinitely for endless lines I don't know where the problem and how it happens. it's the same code, the client sends the input as one line when choosing operation but when I chose insert operation and the server is expecting a value it takes it as infinite endless lines.

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

Server 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.

I think its your Client, in the while loop try to remove the second read from server, it's because you once read all what server sents, and when the loop starts again wants to read from server but there is nothing to read and becomes idle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM