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 );
LVL 2
DaFouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
The loop will block until the socket is closed
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.