0

I have the code below in my server, and when I run it, it will read the input from my client only once and then break out of the try block, and ultimately end the while(true) loop.

public void run() {
      while (true) {
            try {
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); 
                message = bufferedReader.readLine();
                System.out.println("this is message from client: " + message);
            } catch (Exception e) {
                //nope
            }
       //end of while
       break;
       }
}

I want it such that my try block does not finish and that it should always be listening for any incoming messages. If I take the break statement out of the while loop, then I encounter an infinite loop where my message = null. What am I doing wrong?

3
  • 3
    Don't open a new reader at each iteration. Open a BufferedReader once, then loop until readLine() returns null. Commented Aug 11, 2015 at 21:31
  • I think JB Nizet nailed it. That is the real issue. You create a new InputStreamReader and BufferedReader every time through your loop, but those should be moved outside the loop. Also, if you intend for your loop to ever finish you might want to do something like while (message = bufferedReader.readLine() != null) Commented Aug 11, 2015 at 21:34
  • @JBNizet Oh, so that means I only have to open my inputStreamReader (and my outputWriter) just once throughout the program, right? Because then it'll always be open for reading and writing. Commented Aug 11, 2015 at 23:59

2 Answers 2

2

Once a connection has been closed, there will never be a new data i.e. once readLine() returns null that is it, there is no hope there will be more data.

What you should is read data until the connection closes. You cannot prevent the other end from disconnecting.

You should only wrap an input stream once. If you wrap it with a buffered reader multiple times, you will lose data.

I suggest you create your BufferedReader outside the loop and keep looping while you are getting data.

try (BufferedReader br = new BufferedReader(new InputStreaReader(clientSOcket.getInputStream())) {

    for(String message; (message = br.readLine()) != null; ) {
        System.out.println("From client: " + message);
    }

} catch (IOException ioe) {
    ioe.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

1

It's only an infinite loop because you failed to check for end of stream. The readLine() method returns a sentinel value at end of stream: look it up; whereupon you should stop reading.

You're losing data by creating a new BufferedReader per iteration. Don't do that. Use the same one for the life of the socket.

And never ignore exceptions without at least documenting why; and never catch an exception you don't need to catch. The only checked exception that code can throw is IOException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.