[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

readling all of the BufferedReader

Ola,

I want to fill up a string withh all there is in the BufferedReader, But i can only read line by line.
And when I loop through it my code hangs:

please debug this code:

InputStream lInputStream = lSocket.getInputStream();
BufferedReader lBufferedReader = new BufferedReader( new InputStreamReader( lInputStream ) );
               
String lStrRequest = "";
String lStr = lBufferedReader.readLine();
               
while ( lStr != null ) // why is this loop never ending and makes my code hang?
{
        lStrRequest += lStr;
        lStr = lBufferedReader.readLine();
}
System.out.println( lStrRequest );
0
DaFou
Asked:
DaFou
  • 5
  • 3
2 Solutions
 
CEHJCommented:
The loop will block until the socket is closed
0
 
CEHJCommented:
There are three broad ways to make the loop exit

a. arrange the protocol so the sending entity sends an end of transmission marker
b. make the sender close the socket
c. use non-blocking IO
0
 
DaFouAuthor Commented:
Interesting advice.
a. i cant arange the protocol since I am using a standard http webbrowser to connect.
this is the first line of the browser request:
"GET / HTTP/1.1Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*Accept-Language: en-gb"

b. No no closing :-) i am making a chatserver remember?
c. hmm non blocking IO? I am not sure that is usefull and the socket is only used to send info and not read. I only need to read the initial request to determine wether to store the request as a client object or to parse incomming chatmessages from.

I think ill simply read the first 3 or 4 lines using a for ( ) loop
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
Do you just want the headers? If so:

String header = null;
while ((header = lBufferedReader.readLine()) != null) {
      if ((header = header.trim()).length() > 0) {
            // use header
      }
      else {
            break;
      }
      
}
0
 
DaFouAuthor Commented:
and how would i read all of the http request ( including form fields and data ) but break out of the loop once the
lBufferedReader.readLine() does not find anything anymore ( after all of the request was read )?.
0
 
DaFouAuthor Commented:
sorry i think i understand now :-)
0
 
DaFouAuthor Commented:
ill make a new question :-)
0
 
DaFouAuthor Commented:
CEHJ, your code works like a charm, its getting later and i am beginning to make mistakes :)
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now