1

I have here 2 Windows systems in the same network (XP). If I open cmd and I type

ping Computer2

I get an answer (so ping is working). I thought I could do this with Java also, but somehow it's not working:

public static void ping() {

  System.out.println("Ping Poller Starts...");
  final String computer = "Computer2";

  InetAddress inet = null;
  try {
        inet = InetAddress.getByName(computer);
  } catch (UnknownHostException e) {
        e.printStackTrace();
  }
  System.out.println("Sending Ping Request to " + computer);

  boolean status = false;
  try {
        status = inet.isReachable(5000);
  } catch (IOException e) {
            e.printStackTrace();
  }

  if (status)
  {
            System.out.println(computer + " ok");
  }
  else
  {
            System.out.println(computer + " not pingable");
  }
}

Always not pingable. With localhost the code is fine. But with Computer2 I'm not able to ping - but via cmd it's working. Any ideas?

sabisabi
  • 1,501
  • 5
  • 22
  • 40
  • 3
    possible duplicate of [Why does InetAddress.isReachable return false, when I can ping the IP address?](http://stackoverflow.com/questions/9922543/why-does-inetaddress-isreachable-return-false-when-i-can-ping-the-ip-address) – juergen d Feb 22 '13 at 14:00
  • 1
    Have you tried with say `www.stackoverflow.com`? – congusbongus Feb 22 '13 at 14:01
  • Can be many things. `isReachable()` does not even garantee to use ICMP ECHO as the `ping` command does. Try with IP instead of "Computer2". see http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#isReachable(int) – bidifx Feb 22 '13 at 14:06
  • @CongXu no it's not working... just 'localhost' is working via Java. – sabisabi Feb 22 '13 at 14:23

1 Answers1

0

It's not clear if you are pinging the IP address or the computer name, but it could be something as simple as using "\Computer2" instead of "Computer2".

Maloric
  • 5,525
  • 3
  • 31
  • 46
  • I'm pinging the Computername. With \Computer2 or in Java "\\Computer2" is not working: java.net.UnknownHostException – sabisabi Feb 22 '13 at 14:07
  • And how about pinging the IP address? – Maloric Feb 22 '13 at 14:10
  • Oh that's interesting. It's not working even with the ip. Just via cmd. – sabisabi Feb 22 '13 at 14:16
  • Ok your next thing to try should be pinging an external IP address that you know works (try 173.194.34.102, which is google.com). – Maloric Feb 22 '13 at 14:19
  • No this is not even working via cmd. I cannot ping an adress. So 'ping www.stackoverflow.com' or 'ping google.com' is not working even via cmd. Maybe here is something wrong with the network or I dont have permissions. But as I said, I'm able to ping 'Computer2' via cmd – sabisabi Feb 22 '13 at 14:26
  • Ah. Yeah that's a whole other issue by the sounds of it. I would ask if you're connected to the internet but since you're on Stack Overflow we can assume that you are :) Sorry I couldn't help more. – Maloric Feb 22 '13 at 14:30