Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Connection Timing Out

  • 28-01-2013 10:40AM
    #1
    Registered Users, Registered Users 2 Posts: 3,740 ✭✭✭


    Hey, I've just been trying to get back into doing a little coding by making myself a simple java program that will download a set of images from a website. The site basically has a home page with a list of images thumbnails, which on clicked open a second page with the full size image.

    My program can find the links to the page with the full size image, then can find the link to the image itself and then downloads the image. It's all working very nicely bar one or two little things - some of the images don't exist anymore but the thumbnail does - which I've taken into account but the problem is sometimes the image leads to a page which just keeps loading causing my program to time out.

    Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)

    try
    	{			
    	openUrl= new URL(pageUrls[i]);
    	readPage = new BufferedReader(new InputStreamReader(openUrl.openStream()));
    	
    	while((lineReader = readPage.readLine())!=null)
    		{
    		if(lineReader.indexOf("<script type=\"text/javascript\">document.write('<img alt")!=-1)
    			{	
    			openQuote=lineReader.indexOf("+B('")+4;
    			closeQuote=lineReader.indexOf("\')+\'\" />\');");					
    			pageUrls[i]=lineReader.substring(openQuote, closeQuote);
    			}	
    		}
    	readPage.close();
    	}
    
    catch(java.io.FileNotFoundException FileNotFound)
    	{
    	System.out.println("File not Found " + pageUrls[i]);
    	}
    


    This is the piece of code that's causing the problem.
    Is there anyway I can make this code see that the program is taking too long and just skip on??

    The program otherwise works perfectly but I just have no idea how to handle the time out problem so any help would be great


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    Set a connectionTimeout() on the URL connection to a reasonable value. Whatever host you are trying to connect to is timing out.

    Also wrap the initial connection code in its own try catch so that you can catch the timeout and ignore it.


Advertisement