Reputation: 1658
We have a router and 3 PCs connected.
Default gateway: 192.168.1.1
When PC3 tried to find connected PCs using the code from this forum post, it only returns the IP address of the default gateway (That address is the only reachable address).
I tried increasing the timeout for the isReachable()
method. But still it returns only the default gateway address.
I tried doing this to the individual IP addresses.
try {
InetAddress temp2 = InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 1, (byte) 2});
if (temp2.isReachable(1100)) {
java.lang.System.out.println("IP Address: " + temp2.getHostAddress() + " has connection.");
}else{
java.lang.System.out.println("IP Address: " + temp2.getHostAddress() + " has no connection.");
}
} catch (Exception ex) {
java.lang.System.out.println("Error: " + ex.getMessage());
}
Yet doing on those PC1 and PC2 IP addresses, I only got a no connection status. (Which means those IPs are not reachable.)
But when I ping them on my windows console those IPs are connected and pinging is successful.
Upvotes: 3
Views: 699
Reputation: 13967
I believe guido is correct with his theory in the comments. See this question. I realize that the linked answer applies to Linux/UNIX hosts, but I just tested your code on a Windows 7 machine and verified that it, also, sends TCP packets to the echo
port (7) rather than an ICMP echo request. (I tested this under Java version 1.6.0_21
on a Windows 7 box where ver
reported Microsoft Windows [Version 6.1.7600]
)
A typical Windows machine will likely have Windodws Firewall enabled. Therefore, the following sequence of events is likely:
It's hard to say what the "problem with your setup" is. Maybe nothing. It depends on what you're trying to use this functionality for. You might be going about it in the wrong way. You should realize that ping
is not always a 100% accurate way to test if a host is online. In your case it might be more accurate to check the ARP table after trying to ping the host, if you're only interested in hosts on your local network.
Possible workarounds:
ping
command line utility to test reachability.Upvotes: 2