BufferedReader, readLine() method, and the issue

Hi guys, I'm really sorry for this post, but I still couldn't solve my problem. well as API says, BufferedReader readLine() method returns null if end of the stream (EOS) reached. but it waits, here is my code.

the above code listens on port x and reads the Incoming stream from the client(Browser e.g. FF, IE) and parse it (very simple http server).
generally I'm not wondered using BufferedReader, any solution that help me out to solve this problem, I've tried to use InputStreamReader and InputStream classes but I could not.

Here you are reading from Socket, and Socket input stream could be considered as "endless" stream. So readLine will never read null, so to reach line 14 of your code you should use some sort of flag indicating end of the stream.

Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90

posted Sep 20, 2010 07:31:09

0

Hi Andrey Kozhanov.
well because it's a http server I don't have any hand to handle requests from clients, generally clients are browsers, so as you mentioned, how can I determine that browser(client) stream is completed and getting into the line 14? any solution? is there any standard belong browsers to notify servers about complete incoming stream?

Yes, there is. It is the Hypertext Transfer Protocol (HTTP). If you are writing a server, you will want to become very familiar with it.
To answer your question directly, I believe an HTTP request ends with two carriage returns, but I wouldn't bet my life on it.

The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by SP characters. No CR or LF is allowed except in the final CRLF sequence.

well the CRLF is known as "\r\n" and when I tried to determine the end of the stream with this (Just in GET method)

it has the same result, waiting....
but I tried it like this

it seems work fine (but I cannot bet on it) with get method, when for methods which has body it requires some condition of course.
so the new question is, according to http rfc and spec, how to determine it? I mean CRLF.

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90

posted Sep 22, 2010 02:00:23

0

I think I have to swap my eyes
many thanks Joe, I've just got a big help, as you said and according to API the following code may help. does it? I think.