N
N
nano_e_t_42016-12-03 14:59:37
Java
nano_e_t_4, 2016-12-03 14:59:37

How to clean the incoming stream?

Hello everyone
Tell me, plz, how to clean the incoming stream?
there is such a code on the client (it sends data):

outputStream = new PrintWriter(serverConnect.getOutputStream(),true);
    void sendToServer(String message) {
        outputStream.println(message);
        outputStream.flush();

this is the code on the server:
String getValuefromClient() throws IOException {
            clientInputStream = null;
            clientInputStream = new BufferedReader(new InputStreamReader(clientConnect.getInputStream()));
            String message = clientInputStream.readLine();
        return message;

when receiving data in this way, I nullify the buffer. Data received from the client is displayed on the GUI by pressing a button.
actionListener function:
get.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent event) {
               try {
               String message = null;
               message = server.getValuefromClient();
               messages.setText(message);
               } catch (Exception ex) {messages.setText("Problem");
                                       ex.printStackTrace();}
           } 
        });

that is, when I press the get button, I need to wait until the client sends the name.
but when I click, I get the first value sent by the client:
client, sent values:
Peter
Ivan
Alexey
Server, received values ​​when the button is pressed:
Peter
, as far as I understand, this behavior is caused by the fact that when the buffer is created, it clings to the stream, in which there is only the first value sent (because the rest are just dropped):
clientInputStream = new BufferedReader(new InputStreamReader(clientConnect.getInputStream()));

question:
who knows how to clear this stream without breaking the connection with the client?
Thank you

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question