?
Solved

Test for live Internet connection

Posted on 2003-02-26
8
Medium Priority
?
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 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