Solved

url.openStream() bug??

Posted on 2001-07-24
6
234 Views
Last Modified: 2011-10-03
Hello everybody,
I wrote a program that needs a connection to the internet to download some files in order to work properly.  The program periodically attempts to download the files opening a stream to the server using url.openStream(), if an exception occur, the program waits a period of time and then attempts again the connection.

I don't understand why it is behaving different depending on whether the dial-up connection is stablished or not.  If the connection is stablished before the program runs, everithing goes right, but if the program runs before the connection is stablished(at least 1 failed attempt was made for downloading the files) the url.openStream() always thows an exception no matters if the connection is available later on.

It seems to be like it "remembers" that the connection was unavailable and the next time it tries to download the files a real attempt is not made.
Maybe something is happening with the ip address, because it is assigned dinamically by the ISP and in the first case the ip address exists but in the second not until the connection is stablished.

Please, i need a solution as soon as possible.
Tanks,
Diego Broos V.
0
Comment
Question by:dbroos
6 Comments
 
LVL 1

Expert Comment

by:elron
ID: 6313594
Could you post the snippet of code which does this?  I think perhaps you are creating/calling the method in an inappropriate place and it's not getting reinitialized somehow...

(BTW, the word is established, not stablished.  I know many people on here have English as a second language, so no disrespect is meant in the correction...)

Regards,
   elron
0
 

Author Comment

by:dbroos
ID: 6315665
ok and thanks for the correction...

Here is the code:

public class CheckFile implements Runnable {
    ...
    public void run() {
        ...
        while (!stop) {
            try {
                getData();
                Thread.sleep(WAIT);
            } catch (Exception e){
                try {
                    Thread.sleep(WAIT_ERROR);
                } catch(Exception ex) {}
            }
        }
    }

    ...

    private void getData() throws Exception {
        BufferedInputStream bis = null;
        URL url = null;

        try {
            url = new URL(strurl);
            bis = new BufferedInputStream(url.openStream());
            bis.read(arr, 0, arr.length);
        } catch(Exception e) {
            throw e;
        } finally {
            if(bis != null)
                try {
                    bis.close();
                } catch(Exception e) {}
        }
    }
}
0
 
LVL 1

Expert Comment

by:elron
ID: 6315745
A suggestion is to either step through in debug or put in some output statements to see if you are hitting the exception on the "try" or the "finally" in your getData() method.  I suspect it has something to do with the bis.close() in the already connected instance, and the url.openStream() in the not connected instance.

See where the exception is being throws in each instance and we'll work from there...

Regards,
    elron
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:rajesh_bala
ID: 6315923
listening
0
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 70 total points
ID: 6316363
This is a known bug which has been fixed in Merlin (JDK 1.4) - http://developer.java.sun.com/developer/bugParade/bugs/4204351.html

The bug is that InetAddress.getByName() caches not only positive results (host found) but also negative results (host not found).

Sasha Maryanovsky.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 6320690
Btw, it might work if you specify your server(s) by IP and not by hostname.

P.S. Thanks for the points.

Sasha Maryanovsky.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error with Java/Cache JDBC Classpath 2 31
Java JRE greater than 1.6 5 50
custom annotations 9 32
fibonacci ten numbers 4 28
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now