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.