Mike
Mike

Reputation: 1139

Java InetAddress failed

I am trying to check if some host is reachable using the "isReachable" method.

line 113: oaiBaseURL = "http://www.cnn.com";//////////////////////////////////////
line 114: boolean res = InetAddress.getByName(oaiBaseURL).isReachable(10000);
line 115: System.out.println("------reachable:"+res);

and get the following error message (in eclipse):

java.net.UnknownHostException: http://www.cnn.com
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
    at java.net.InetAddress.getAddressFromNameService(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at com.irWizard.web.validator.WizardValidator.validateForm(WizardValidator.java:114)

Does anyone understand what might be the reason for this error?

Thanks in advance!

Upvotes: 2

Views: 7530

Answers (5)

Sankarganesh Eswaran
Sankarganesh Eswaran

Reputation: 10375

Adding the entry in /etc/hosts with relevant hostname & ip address resolved in my case.

Upvotes: 0

Laxminarayan
Laxminarayan

Reputation: 198

I tried this on my laptop by creating hotspot and it worked. Sometimes there may be problem in giving the subnet input to the program at that time you may face this problem. Hope it will work for you.

subnet = subnet.trim();
int timeout = 1500;
for(int i=1;i<254;i++)
    {
    try
      {
        String host = subnet +"."+i;
        if (InetAddress.getByName(host).isReachable(timeout))
          {
            Check = Check+host+"\n";
            System.out.println(host);
          } 

       }
    catch (UnknownHostException ex) { 
        Logger.getLogger(WiFi.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(WiFi.class.getName()).log(Level.SEVERE, null, ex);

Upvotes: 0

Omga
Omga

Reputation: 11

did you parsed the url as it should ? you need to get the host name only from URL

URL oaiBaseURL = new URL("http://www.cnn.com");
boolean res = InetAddress.getByName(oaiBaseURL.getHost()).isReachable(10000);
System.out.println("------reachable:"+res); 

Upvotes: 0

jagmohan
jagmohan

Reputation: 2052

Quote from documentation:

The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

You don't need to specify scheme. Just remove "http://" and it should work.

Upvotes: 0

jbx
jbx

Reputation: 22178

You need to remove the http:// prefix.

As far as I know the InetAddress.getByName() method takes a hostname not a URL.

You can change the code as follows:

   URL url = new URL("http://www.cnn.com");
   boolean res = InetAddress.getByName(url.getHost()).isReachable(10000);
   System.out.println("------reachable:"+res);

However keep in mind the mechanisms the method isReachable() uses to determine whether it is reachable or not. It uses mostly ICMP techniques, which a lot of websites or intermediate firewalls might block.

Upvotes: 7

Related Questions