如何等待客户端处理程序上的其他客户端输入?插座

How can I wait for additional client input on client handler ? Socket

第一次尝试一切正常并按预期更改值,但是当循环结束并且客户端重新启动并且应该能够进行新输入时,连接线程变得与客户端输入异步并且请求字符串被分配给其他的。

当客户端启动时,应该有名称和 hours/date 的输入,但是在连接中第 97 行左右完成预订后,输入和输出是错误的,它们不匹配。我认为这是由于要求额外输入以确认预订造成的。或者我错误地循环 /return 到开始。 这是客户端处理程序 class

public class Connection extends Thread {
private Socket socket;
private Data Ledger;
private Reservation that_r;
private Integer month;
private Integer day;
private Integer hour;

public Connection(Socket socket, Data Ledger) {
    this.socket = socket;
    this.Ledger = Ledger;
}
@Override
synchronized public void run() {
    try {
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);




        while(true) {
            String request = in.readLine();
            if(request.equals("exit")) {
                break;
            }

            do{
                Boolean  completion = Boolean.FALSE;

                System.out.println("Received client input: " + request);
                out.println("Write Day + Hour you want to reserve");


                String R_name =  in.readLine();
                System.out.println("name " + R_name);
                String R_month =  in.readLine();
                System.out.println("month "+R_month);
                String R_day =  in.readLine();
                System.out.println("day "+R_day);
                String R_hour =  in.readLine();
                System.out.println("hour "+R_hour);

                out.println("Checking for existing reservation");

                try{this.month = Integer.parseInt(R_month);}
                catch (Exception e){System.out.println("sth wrong"+e.getMessage());out.println("Wrong number"); }
                try{this.day = Integer.parseInt(R_day);}
                catch(Exception e){ System.out.println("sth wrong "+e.getMessage());out.println("Wrong number"); }
                try{this.hour = Integer.parseInt(R_hour);}
                catch (Exception e){System.out.println("sth wrong "+e.getMessage());out.println("Wrong number");}

                try {
                    if (Ledger.findReservation(month, day, hour).getReserved()){
                        System.out.println(Ledger.reservations);
                        out.println("Currently being reserved sorry");
                        out.println("press Enter");
                        out.println("END");
                        return;
                    }
                }catch (NullPointerException e){
                    this.that_r = new Reservation(R_name,month,day,hour,true);
                    Ledger.reservations.add(that_r);
                    out.println("Checking if available");
                    try {if (Ledger.findDay(month, day).aval(hour)){
                        out.println("Can be reserved, RESERVE Y/N");
                        completion = true;
                        out.println("END");
                      }else{
                        out.println("Sorry it is not available but you know that already as you can see all free termines");
                        out.println("END");
                        return;
                      }
                    }catch (NullPointerException f){
                        out.println("Day you want to reserve is not here");
                    }
                }
                if (completion){
                    String answer = in.readLine();
                    System.out.println(answer);
                    if (answer.equals("y")) {
                        Ledger.findDay(month,day).changeaval(hour,Boolean.FALSE);
                        //out.println("END");


                    }
                    if (answer.equals("n")) {
                        Ledger.reservations.remove(that_r);
                    }
                    if (answer.equals("")){
                        break;
                    }
                }
            }while (request.equals("r"));

            if (request.equals("show")){
                out.println(LocalDateTime.of(2019,1,5,23,55));
                out.println("END");
                out.flush();

            }
            }

    } catch(IOException e) {
        System.out.println(e.getMessage());
    } finally {
        try {
            socket.close();
            for (Day x: Ledger.ledger){
                System.out.println(x.getDay()+" / "+ x.getMonth());
                System.out.println(x.getSchedule());
            }

            for (Reservation x: Ledger.reservations){
                System.out.println(x.getDay()+" / "+ x.getMonth());
                System.out.println(x.getName()+" "+ x.getHour());
            }

        } catch(IOException e) {
            System.out.println(e.getMessage());
        }
    }

}
}

然后是客户端

public class Client {

public static void main(String[] args) {
    try (Socket socket = new Socket("localhost", 5000)) {
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        Scanner scanner = new Scanner(System.in);
        String input;
        do {
            System.out.println("r for reserving ");
            System.out.println("show for showing reservations");

            input = scanner.nextLine();
            out.println(input);
            out.flush();

            if (input.equals("r")) {
                System.out.println(in.readLine());
                System.out.println("name");
                String new_name = scanner.nextLine();
                out.println(new_name);

                System.out.println("month");
                String new_month = scanner.nextLine();
                out.println(new_month);

                System.out.println("day");
                String new_day = scanner.nextLine();
                out.println(new_day);

                System.out.println("hour");
                String new_hour= scanner.nextLine();
                out.println(new_hour);

                System.out.println("wait");

                listen(in);

                String answer = scanner.nextLine();
                System.out.println("answered " + answer);
                out.println(answer);
            }
        } while (!input.equals("exit"));
    } catch (IOException e) {
        System.out.println("Client Error: " + e.getMessage());
    }
}


public static void listen(BufferedReader in) {
    while (true) {
        try {
            String p = in.readLine();
            if (p.equals("END")) {
                break;
            }else{System.out.println(p);}

        }catch (IOException e){
            System.out.println(e.getMessage());
        }


    }
}
}

我希望客户端在预订失败或完成预订后或在其他一些过程后进行新输入,以便连接客户端处理程序看到并再次分配输入。

我建议封装所有预订信息,而不是发送几条单独的信息,这样可以避免在多个客户端连接并发送他们的预订时保留订单带来的麻烦。例如。 Reservation reservation1 = new Reservation(name, month, hour).然后只需将其全部发送到一个块中:out.write(reservation1.getBytes())