Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6247011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:47:30+00:00 2026-05-24T12:47:30+00:00

My issue is fairly straightforward. My program requires immediate notification if a network connection

  • 0

My issue is fairly straightforward. My program requires immediate notification if a network connection is lost. I’m using Java 5, so I’m unable to use the very handy features of NetworkInterface.

Currently I have two different methods of checking for a network connection:
(try..catches removed)

Method One:

URL url = new URL("http://www.google.com");
HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection();
// trying to retrieve data from the source. If offline, this line will fail:
Object objData = urlConnect.getContent();
return true;

Method 2:

Socket socket = new Socket("www.google.com", 80);
netAccess = socket.isConnected();
socket.close();
return netAccess;

However, both of these methods block until a Timeout is reached before returning false. I need a method that will return immediately, but is compatible with Java 5.

Thanks!

EDIT:
I forgot to mention that my program is reliant on IP addresses, not DNS names. So checking for an error when resolving a host is out…

2nd EDIT:

Figured out a final solution using NetworkInterface:

Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces();
        while(eni.hasMoreElements()) {
            Enumeration<InetAddress> eia = eni.nextElement().getInetAddresses();
            while(eia.hasMoreElements()) {
                InetAddress ia = eia.nextElement();
                if (!ia.isAnyLocalAddress() && !ia.isLoopbackAddress() && !ia.isSiteLocalAddress()) {
                    if (!ia.getHostName().equals(ia.getHostAddress()))
                        return true;
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:47:31+00:00Added an answer on May 24, 2026 at 12:47 pm

    From JGuru

    Starting with Java 5, there is an isReachable() method in the
    InetAddress class. You can specify either a timeout or the
    NetworkInterface to use. For more information on the underlying
    Internet Control Message Protocol (ICMP) used by ping, see RFC 792
    (http://www.faqs.org/rfcs/rfc792.html).

    Usage from Java2s

      int timeout = 2000;
      InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
      for (InetAddress address : addresses) {
        if (address.isReachable(timeout))
          System.out.printf("%s is reachable%n", address);
        else
          System.out.printf("%s could not be contacted%n", address);
      }
    

    If you want to avoid blocking use Java NIO (Non-blocking IO) in the java.nio package

    String host = ...; 
    InetSocketAddress socketAddress = new InetSocketAddress(host, 80); 
    channel = SocketChannel.open(); 
    channel.configureBlocking(false); 
    channel.connect(socketAddress);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fairly simple issue which is solved in PHP by using a static variable. private
I'm attempting to use MIB files in PySNMP. The code is fairly straightforward. Nothing
I am fairly new to using infragistics controls (started yesterday). While they are (very)
I know this is a fairly contentious issue amongst programmers, but when developing I
I'm fairly new to SVN and I have a weird behavior issue. I'm working
We're having an issue on one of our fairly large websites with spam bots.
I have what I think should be a fairly simple mapping issue, but not
I have an issue with .Net reflection. The concept is fairly new to me
Seems like a fairly simple issue but can't get it to work. I am
Example page , Accompanying CSS Should be a fairly basic issue but for some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.