Link to home
Start Free TrialLog in
Avatar of DaFou
DaFou

asked on

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 );
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of DaFou
DaFou

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DaFou

ASKER

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 )?.
Avatar of DaFou

ASKER

sorry i think i understand now :-)
Avatar of DaFou

ASKER

ill make a new question :-)
Avatar of DaFou

ASKER

CEHJ, your code works like a charm, its getting later and i am beginning to make mistakes :)