简体   繁体   English

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

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

On first try everything works properly and changes values as it should, but when loop is finished and client restarts and one should be able to make new inputs, Connection thread becomes asynchronized with client inputs and request string is being assigned to sth else. 第一次尝试时,一切正常工作并按应有的方式更改值,但是当循环完成并且客户端重新启动并且应该能够进行新输入时,Connection线程将与客户端输入异步,并且将请求字符串分配给其他线程。

As the client starts there should be inputs for name and hours/date but after complete reservation around line 97 in Connection, input and output is wrong they get mismatched. 当客户端启动时,应该输入名称和小时/日期,但是在Connection中第97行周围完全保留之后,输入和输出错误,它们将不匹配。 I think it is caused by asking for additional input to confirm reservation. 我认为这是由于要求其他输入以确认预订而引起的。 or I incorrectly loop /return to begining. 或我错误地循环/返回开始。 This is Client Handler 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());
        }
    }

}
}

And then there is Client side 然后是客户端

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


    }
}
}

I want to client make new inputs after failed reservation or completed reservation or after some other process so that connection- client handler see that and also assigns inputs again. 我想在失败的保留或完成的保留之后或在其他一些过程之后,让客户端进行新的输入,以便连接客户端处理程序可以看到并重新分配输入。

Instead of sending several individual pieces of information, I would recommend encapsulating all the reservation info and avoid the headache that comes with preserving the order should multiple clients connect and send their reservations. 我建议不要封装一些单独的信​​息,而是建议封装所有的预订信息,并避免在多个客户端连接并发送其预订时保存订单所带来的麻烦。 Eg Reservation reservation1 = new Reservation(name, month, hour). 例如,保留预订1 =新的保留(名称,月份,小时)。 Then simply send it all in one chunk: out.write(reservation1.getBytes()) 然后将其全部发送到一个块中:out.write(reservation1.getBytes())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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