Hel Hel - 1 year ago 87
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

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download