1

I wrote the following code to try a ping. But as I run it, the following exception gets thrown :

java.net.UnknownHostException: http://localhost:8084/server/index.jsp
    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.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at Tester.main(Tester.java:10)

import java.net.InetAddress;

class Tester {
public static void main(String args[]) {
    try {
      InetAddress address = InetAddress.getByName("http://localhost:8084/server/index.jsp");
      boolean isReachable = address.isReachable(2000);
      if(isReachable)
        System.out.println("The address is reachable");
      else
        System.out.println("The address is not reachable");

    } catch(Exception exc) {
       exc.printStackTrace();
      }
}
}

Why is it so ? The server is running and the page is opening fine in the web-browser.

saplingPro
  • 20,769
  • 53
  • 137
  • 195
  • InetAddress.getByName("host") accept a host name and not his protocol. For example if your host is: "localhost:8084/server/abc/page.jsp" this works – carminePat Jan 17 '13 at 11:43

3 Answers3

5

The problem is in this line:

InetAddress address = InetAddress.getByName(
        "http://localhost:8084/server/index.jsp");

The InetAddress.getByName(String) method requires a hostname. You've given it a URL string. The hostname component of that address is "localhost".

If you want to "ping" the host associated with a URL, then you need to parse the URL and extract the hostname component something like this:

String hostname = new URL(str).getHost();

But you need to deal with the cases where the URL is malformed, or where it doesn't have a host name component.


I imagine that you are actually trying to test some other hostname, because sending an ICMP_PING request to "localhost" (typically 127.0.0.1) is kind of pointless.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • The network address of this _localhost_ could look like : `http://192.168.43.187:8084/server/index.jsp` . How do the clients initially connected to the server, check again if they are connected to the server ? So I was trying to do this. – saplingPro Dec 10 '12 at 12:59
  • How do a client ping for the address : `http://192.168.43.187:8084/server/index.jsp ` ? – saplingPro Dec 10 '12 at 13:00
  • 1
    1) The name "localhost" is normally bound to a lookback address such as `127.0.0.1. If it is bound to a non-loopback address, there is something a bit broken about your network configuration. 2) See the updates to my answer on how to extract the hostname from a URL. 3) Please STOP referring to a URL as an address. It is a resource locator ... not an address. – Stephen C Dec 10 '12 at 13:04
0

Because the domain behind a firewall which blocks the ping request

Shantanu Banerjee
  • 1,417
  • 6
  • 31
  • 51
0

getByName take a host name, or IP addresse as parameter, not e URL.

Houari
  • 5,326
  • 3
  • 31
  • 54