Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

BufferedReader, readLine() method, and the issue

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

my config: winxp sp2(sorry),tested in both JDK1.6.0.20 and Apache harmony 6M2 (same result).
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jason Marlyn
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arash M. Dehghani wrote: 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.
 
Jason Marlyn
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Joe, thanks for helping, well I read the HTTP rfc and it says

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.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be interested in the JavaDoc for BufferedReader:


public String readLine() throws IOException

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



 
Jason Marlyn
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks again.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic