Java, sockets, BufferedReader and readline hang ... :(

I am not a Java programmer at all. I try to avoid it at all costs in fact, but it is required that I use it for the class (in a school sense). The teacher requires us to use Socket (), BufferedReader (), PrintWriter () and other other things, including the BufferedReader () readLine () method.

Basically, this is the problem I am facing. The documentation clearly states that readLine should return zero at the end of the input stream, but this is not what happens.

Socket link = new Socket(this.address, 80); BufferedReader in = new BufferedReader( new InputStreamReader( link.getInputStream() )); PrintWriter out = new PrintWriter( new PrintWriter( link.getOutputStream(), true )); out.print("GET blah blah blah"); // http request by hand out.flush(); // send the get please while( (s=in.readLine()) != null ) { // prints the html correctly, hooray!! System.out.println(s); } 

Instead of ending at the end of the HTML, I get an empty string, 0 and another empty string, and then the next in.readLine () hangs forever. What for? Where is my null?

I tried out.close () to find out if Yahoo! did a permanent http session or something (which I don’t think that without a header we are ready to do this).

All the examples of Java sockets that I find on the network seem to indicate while the loop is the correct form. I just don't know enough Java to debug this.

+5
source share
4 answers

Your problem is the chunked content encoding. This is used when the length of the content requested from the web server is unknown at the time the response is started. It basically consists of the number of bytes sent, followed by the CRLF , followed by the bytes. The end of the response is signaled by the exact sequence that you see. The web server is now waiting for your next request (also called "pipelining the request").

You have several options:

  • Use HTTP version 1.0. This will cause the web server to automatically close the connection when the response is complete.
  • When sending a request, specify the heading "Connection: close". This will also close the connection.
  • Correctly encode the contents of the "chunked" encoding and just treat it as if the answer is complete, and that it is.
+8
source

So you are reading a socket (you are not showing this in your code, but what am I collecting from the text)?

Until the other side closes the connection, Java does not know that this is at the end of the input, so readLine() expects the other side to send more data and not return null .

+7
source

Try the GET url HTTP/1.0 . HTTP/1.0 tells the server that you cannot process more than one document for each connection. In this case, the server should close the connection after sending the result to you.

+4
source

Your HTTP request is not complete without two carriage + line return pairs. You should probably also call after sending the request:

 out.print("GET /index.html HTTP/1.0\r\n"); // maybe print optional headers here // empty line out.print("\r\n"); out.flush(); out.close(); 
0
source

All Articles