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

时间:2019-01-07 15:40:03

标签: java sockets client serversocket

首先尝试一切正常,并按需要更改值,但是当循环完成并且客户端重新启动并且应该能够进行新输入时,连接线程将与客户端输入异步,并且将请求字符串分配给其他

在客户端启动时,应该输入名称和小时/日期,但是在Connection中第97行周围完全保留之后,输入和输出错误,它们将不匹配。我认为这是由于要求其他输入以确认预订而引起的。或我错误地循环/返回开始。 这是客户端处理程序类

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


    }
}
}

我想在失败的保留或完成的保留之后或在其他一些过程之后,让客户端输入新的信息,以便连接客户端处理程序看到该信息并再次分配输入。

1 个答案:

答案 0 :(得分:0)

我建议不要封装多个单独的信息,而是建议封装所有的预订信息,并避免在多个客户端连接并发送其预订时保存订单所带来的麻烦。例如。预订Reservation1 =新的Reservation(名称,月份,小时)。然后,将其全部发送到一个块中:out.write(reservation1.getBytes())