Salve, scrivo perchè ho un problema con un BufferedReader, ossi vorrei sapere come fare per "svuotarlo" ad un certo punto nel codice. In particolare ho un problema con un socket TCP:
Ho fatto un piccolo codice di esempio che riassume la situazione, il server ha questo comportamento
Ossia invia 4 stringhe alla connessione (di cui solo la prima ha interesse) e poi un altra stringa quando riceve la stringa "query"codice:public static void main(String argv[]) throws Exception { ServerSocket welcomeSocket = new ServerSocket(6789); while (true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream out = new DataOutputStream(connectionSocket.getOutputStream()); out.writeBytes("Important Message\n"); out.writeBytes("Not important Message 1\n"); out.writeBytes("Not important Message 2\n"); out.writeBytes("Not important Message 3\n"); if ("query".equals(in.readLine())) { out.writeBytes("Data\n"); } } }
Il client è cosi:
Mi manca il codice da inserire al posto del commento per scartare i vari "Not important Message" alla connessione.codice:public static void main(String argv[]) throws Exception { String message; try (Socket clientSocket = new Socket("localhost", 6789)) { DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); message = in.readLine(); System.out.println(message); // Qualcosa per svuotare il BufferedReader in Thread.sleep(500); out.writeBytes("query\n"); message = in.readLine(); System.out.println("Received::" + message); } }
Qualcuno sa aiutarmi?
Grazie in anticipo.

Rispondi quotando


