Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Connection Timing Out

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


    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 Posts: 2,031 ✭✭✭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