1

I use the following code to output HTTP request message. But the loop never ends after outputing HTTP request from browser. It seems that bufferReader just waiting for new bytes after outputing HTTP request.

InputStream inputStream = socket.getInputStream(); // socket is returned by 
                                                   // serverSocket.accept()
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

// output HTTP request              
String line;
while ((line = bufferedReader.readLine()) != null) { // the loop never ends 
    System.out.println(line);       
}
Hel
  • 305
  • 5
  • 14

2 Answers2

1

I agree with mhasan. If you know ahead of time the expected length of the message, you can read that many bytes and then explicitly exit the loop.

See the code examples in this answer: https://stackoverflow.com/a/19863726/2751039

Community
  • 1
  • 1
Matt
  • 828
  • 8
  • 25
0

This is happening because the peer connection is never getting closed. If it never closes, readLine() won't return null.

mhasan
  • 3,703
  • 1
  • 18
  • 37