How to ping an IP address
Check your connectivity. On my Computer this prints REACHABLE for both IP's:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 173.194.32.38
Host is reachable
EDIT:
You could try modifying the code to use getByAddress() to obtain the address:
public static void main(String[] args) throws UnknownHostException, IOException {
InetAddress inet;
inet = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
inet = InetAddress.getByAddress(new byte[] { (byte) 173, (byte) 194, 32, 38 });
System.out.println("Sending Ping Request to " + inet);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
The getByName() methods may attempt some kind of reverse DNS lookup which may not be possible on your machine, getByAddress() might bypass that.
You can not simply ping in Java as it relies on ICMP, which is sadly not supported in Java
http://mindprod.com/jgloss/ping.html
Use sockets instead
Hope it helps
InetAddress.isReachable()
according to javadoc:
".. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host..".
Option #1 (ICMP) usually requires administrative (root)
rights.
I think this code will help you:
public class PingExample {
public static void main(String[] args){
try{
InetAddress address = InetAddress.getByName("192.168.1.103");
boolean reachable = address.isReachable(10000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e){
e.printStackTrace();
}
}
}