Detecting end of an input stream. Checking for -1 does not appear to work.
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Michael_Link
Posted On:   Thursday, November 28, 2002 03:17 AM

Simple question. When reading from a client socket, checking for -1 works seldom. (The socket is connect to an HTTP server - Apache). The code below get some bytes and then hangs in the while loop forever. InputStream is = socket.getInputStream(); while ((byteRead = inputStream.read()) != -1) { log("HTTP.run() bytes "+i+" = "+byteRead); } What is the correct way to detect the end of an InputStream when reading from a socket? Thanks, Mike    More>>

Simple question. When reading from a client socket, checking for -1 works seldom. (The socket is connect to an HTTP server - Apache).


The code below get some bytes and then hangs in the while loop forever.



			
				
InputStream is = socket.getInputStream();

while ((byteRead = inputStream.read()) != -1) {
log("HTTP.run() bytes "+i+" = "+byteRead);
}



What is the correct way to detect the end of an InputStream when reading from a socket?


Thanks,
Mike

   <<Less

Re: Detecting end of an input stream. Checking for -1 does not appear to work.

Posted By:   Michael_Link  
Posted On:   Thursday, November 28, 2002 04:29 AM

Thanks for the tips.


The problem is now located and it appears to relate to the way the web server handles the client socket/connection.


There is only a -1 on the stream if the web server has closed the connection.


Of course, most modern webservers are configured to use keep-alive if no Connection: close HTTP header is sent to them.


The connection therefore does not get closed until some inactivity timeout occurs on the web server, so the code above gets stuck in a loop for a while.


Mike

Re: Detecting end of an input stream. Checking for -1 does not appear to work.

Posted By:   Gautam_Marwaha  
Posted On:   Thursday, November 28, 2002 04:01 AM

shd work. post your code...

Re: Detecting end of an input stream. Checking for -1 does not appear to work.

Posted By:   Gautam_Marwaha  
Posted On:   Thursday, November 28, 2002 04:01 AM

shd work. post your code...
About | Sitemap | Contact