You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Michael Partheil <mi...@gmx.de> on 2007/08/19 21:46:20 UTC

Detecting lost network connection?!

Hi!

I'm having a problem with detecting when the computer my program is  
running on loses its connection to the internet/server while  
streaming a HTTP-Response Body.
To be concrete: I'm receiving a file via HTTP-GET and then simply  
disconnect the computer from the internet. My program stops receiving  
anything but does not throw a SocketTimeoutException or something  
similar although I've set all (?) the necessary parameters on the  
HttpClient and GetMethod (please see sample code below!). I thought  
that something like
client.getHttpConnectionManager().closeIdleConnections(10000); would  
do the job but it doesn't. In fact I see something like the following  
in the log?!
[DEBUG] IdleConnectionHandler - Checking for connections,  
idleTimeout: 1187551910312

The sample code below reproduces the problem: It simply sets up a  
HttpClient+GetMethod with an url representing an indefinitely large  
mp3 file (internet radio stream) and starts a Thread which reads  
data. If you then disconnect your computer the Thread doesn't stop  
with an exception.


Java version: 1.5.0_07, Apple Computer, Inc.
Operating system: Mac OS X 10.4.10
HttpClient version: 3.0.1


Thanks a lot!
Michael Partheil


===  Sample code:
public class HttpClientTest
{
	private static final String URL = "http:// 
d85195728.i.tis.core005.cdn.streamfarm.net:80/17000hr/live/3435hr3/ 
de_96.mp3";
	
	public static void main(String... args) throws Exception
	{
		HttpClient client = new HttpClient(new  
MultiThreadedHttpConnectionManager());
		client.getHttpConnectionManager().closeIdleConnections(10000);
		client.getHttpConnectionManager().getParams().setSoTimeout(5000);
		client.getParams().setSoTimeout(5000);
		client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS,  
10);
		client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
	
		GetMethod get = new GetMethod(URL);
		get.getParams().setSoTimeout(5000);
		get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
		get.setFollowRedirects(true);
		get.addRequestHeader("User-Agent", "WinAmp");
		
		client.executeMethod(get);
		
		InputStream in = get.getResponseBodyAsStream();
		(new ReaderThread(in)).start();
	}
	
	private static class ReaderThread extends Thread
	{
		private final InputStream in;
		
		public ReaderThread(InputStream in)
		{
			this.in = in;
		}
		public void run()
		{
			try
			{
				while (! isInterrupted())
				{
					try
					{
						Thread.sleep(100);
					}
					catch (InterruptedException e)
					{
						interrupt();
						return;
					}

					try
					{
						int toRead = in.available();
						if (toRead == 0)
							continue;
						byte[] buffer = new byte[toRead];
						int nRead = in.read(buffer, 0, toRead);
						if (nRead == -1)
							return;
						assert nRead == toRead;
						
						System.out.println("read: "+nRead);
					}
					catch (IOException e)
					{
						e.printStackTrace();
						return;
					}
				}
			}
			finally
			{
				System.out.println("ReaderThread finished");
			}
		}
	}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Detecting lost network connection?!

Posted by Roland Weber <os...@dubioso.net>.
Hi Michael,

> I'm receiving a file via HTTP-GET and then simply
> disconnect the computer from the internet. My program stops receiving
> anything but does not throw a SocketTimeoutException or something
> similar although I've set all (?) the necessary parameters on the
> HttpClient and GetMethod (please see sample code below!).

The actual read operation on the socket calls somewhere into
native code. If the TCP/IP subsystem of your machine and/or
of the JVM you are using does not detect the lost connection
and trigger an error, then HttpClient can do nothing about it.
Have you checked whether other users of the Mac JDK have
reported similar problems, not necessarily with HttpClient?

> I thought that something like
> client.getHttpConnectionManager().closeIdleConnections(10000); would do
> the job but it doesn't.

A connection that is allocated by your application is not idle.
Idle connections are those that have been returned by applications.

A workaround would be to start a second thread that monitors
the application and calls method.abort() if the download stalls.
Check the stack trace of the exception that is triggered to see
where exactly the thread was blocked.

>         client.getHttpConnectionManager().getParams().setSoTimeout(5000);
>         client.getParams().setSoTimeout(5000);
>         get.getParams().setSoTimeout(5000);

Doesn't look like you missed one.

cheers,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org