?
Solved

Test for live Internet connection

Posted on 2003-02-26
8
Medium Priority
?
198 Views
Last Modified: 2010-03-31
Anybody have some simple code that tests for a live internet connection?  I've been playing around with the URL class and the URLConnection class, but I can't find anything that tests for a connection.  Here is some code I have so far if it helps.

URL url = null;
         
try
{
      url = new URL("http://www.yahoo.com");
}
catch(MalformedURLException me){}

try
{
      URLConnection urlC = url.openConnection();
}
catch(IOException ie){}

This code works either way, even if I'm not connected to the Internet, but I don't know if I am even close or not.
0
Comment
Question by:sre23
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:ykaganov
ID: 8029999
See if this helps. http://www.3dcom.com/ping/PingDisplay.html

I have no idea why that code is on a transgender website :))

There's more links at http://www.ping127001.com/pingpage.htm
0
 

Expert Comment

by:apocalypso79
ID: 8030075
How do you know if it works or fails ?

Try printing out something in the catch section.

try
{
     url = new URL("http://www.yahoo.com");
}
catch(Exception e){
   e.printStackTrace();
   System.out.println("e.getMessage());
}

I think you'll find it's failing.
0
 
LVL 1

Author Comment

by:sre23
ID: 8030196
I see what you are saying about printing information from the catch statement.  The way I checked it was by just setting a break point and stepping through the code in debug mode.  No exceptions were ever thrown so I know the code would never reach the catch section.  This is just something I started working on today, and I'm not really sure where to start.  I don't know if the code I have even comes close to what I am trying to do.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:apocalypso79
ID: 8030343
Hi,

You're right..the URL class is funny.:)

If what you want to do is simply check for a internet connection, I think the Socket class should work too.

Instantiate a socket and try to connect to a well-known port (port 80/23 on something you know is up).

Let the success or failure decide wether you have a connection or not. Think this should suffice though it's not a perfect test.


0
 
LVL 1

Expert Comment

by:ykaganov
ID: 8030364
If you want to user URL, do this
        URL url = new URL("http://www.yahoo.com");
        URLConnection conn = url.openConnection();
        conn.connect();
        ((HttpURLConnection)conn).disconnect();
       
    }
    catch(Exception e){
      e.printStackTrace();
      System.out.println(e.getMessage());
}

But I don't recommend it unless you explicitly want to check if you can connect to yahoo.com because you will get false negatives if yahoo.com goes down. The ping solution that I posted above should work...
0
 
LVL 1

Expert Comment

by:ykaganov
ID: 8030368
If you want to user URL, do this
        URL url = new URL("http://www.yahoo.com");
        URLConnection conn = url.openConnection();
        conn.connect();
        ((HttpURLConnection)conn).disconnect();
       
    }
    catch(Exception e){
      e.printStackTrace();
      System.out.println(e.getMessage());
}

But I don't recommend it unless you explicitly want to check if you can connect to yahoo.com because you will get false negatives if yahoo.com goes down. The ping solution that I posted above should work...
0
 
LVL 3

Accepted Solution

by:
NetWize earned 80 total points
ID: 8032053
But:
URLConnection is designed to work over proxies and sockets dont. So if you're checking the internet connection in any company big enough to run a proxy server and a firewall, sockets will fail.

You could "optimize" the code calling

new URL("http://www.google.com").openStream();

which includes the connect().

0
 
LVL 1

Author Comment

by:sre23
ID: 8036197
Yep this worked, and the program will be running on various networks which probably are running either proxy or a firewall.  The code you gave me (new URL("http://www.google.com").openStream() returns an InputStream object if you have a live connection, or it throws an IOException if you don't.  Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This video teaches viewers about errors in exception handling.
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 …
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

579 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