Hel Hel - 1 month ago 7
Java Question

How to output HTTP request message correctly

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

Answer

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