Java Code Examples for java.net.InetAddress.isReachable()

The following are Jave code examples for showing how to use isReachable() of the java.net.InetAddress class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: home-automation   File: AsyncNetworkRequest.java   Source Code and License Vote up 11 votes
@Override
protected String doInBackground(String... strings) {
    try {
        String address = strings[0];
        InetAddress inetAddress = InetAddress.getByName(address);
        if (inetAddress != null && inetAddress.isReachable(100)){
            ip = inetAddress.getHostAddress();
            deviceFound = true;
            selfFound = selfIp.equals(ip);

            if (!selfFound) {
                NbtAddress[] addressList = NbtAddress.getAllByAddress(address);
                NbtAddress nbtAddress = addressList[0];
                if (address != null) {
                    deviceName = nbtAddress.getHostName();
                }
            }
        }
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
    return null;
}
 
Example 2
Project: hekate   File: HostReachabilityDetector.java   Source Code and License Vote up 7 votes
@Override
public boolean isValid(ClusterNode localNode) {
    try {
        InetAddress address = InetAddress.getByName(host);

        boolean reachable = address.isReachable(timeout);

        if (reachable) {
            if (DEBUG) {
                log.debug("Address reachability check success [host={}, timeout={}]", host, timeout);
            }
        } else {
            log.warn("Address reachability check failed [host={}, timeout={}]", host, timeout);
        }

        return reachable;
    } catch (IOException e) {
        log.warn("Address reachability check failed with an error [host={}, timeout={}, cause={}]", host, timeout, e.toString());
    }

    return false;
}
 
Example 3
Project: mokka7   File: PingWatchdogService.java   Source Code and License Vote up 6 votes
private boolean isReachable(InetAddress address, int timeOutMillis) throws Exception {
	if (isWindowsOS) {
		return address.isReachable(timeOutMillis);
	}
	Process p = new ProcessBuilder("ping", "-c", "1", address.getHostAddress()).start();
	try {
		p.waitFor(timeOutMillis, TimeUnit.MILLISECONDS);
	} catch (InterruptedException e) {
		return false;
	}
	if (p.isAlive()){
		return false;
	}
	int exitValue = p.exitValue();
	logger.trace("exit value: {}", exitValue);

	return exitValue == 0;
}
 
Example 4
Project: jaer   File: SpiNNaker_InterfaceFactory.java   Source Code and License Vote up 6 votes
private void pingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pingButtonActionPerformed
       String host = hostTF.getText();
     try {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        InetAddress adr = Inet4Address.getByName(host);
        try{
            adr.isReachable(3000);
            JOptionPane.showMessageDialog(this, host+" is reachable, but it may not be the SpiNNaker!");
        }catch(IOException notReachable){
            JOptionPane.showMessageDialog(this, host+" is not reachable: "+notReachable.toString(), "Not reachable", JOptionPane.WARNING_MESSAGE);
        }
    } catch (UnknownHostException ex) {
              JOptionPane.showMessageDialog(this, host+" is unknown host: "+ex.toString(), "Host not found", JOptionPane.WARNING_MESSAGE);
    } finally {
        setCursor(Cursor.getDefaultCursor());
    }

}
 
Example 5
Project: jaer   File: eDVS128_InterfaceFactory.java   Source Code and License Vote up 6 votes
private void pingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pingButtonActionPerformed
    String host = hostTF.getText();
    try {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        InetAddress adr = Inet4Address.getByName(host);
        try {
            adr.isReachable(3000);
            JOptionPane.showMessageDialog(this, host + " is reachable. However it may not be the eDVS!");
        } catch (IOException notReachable) {
            JOptionPane.showMessageDialog(this, host + " is not reachable: " + notReachable.toString(), "Not reachable", JOptionPane.WARNING_MESSAGE);
        }
    } catch (UnknownHostException ex) {
        JOptionPane.showMessageDialog(this, host + " is unknown host: " + ex.toString(), "Host not found", JOptionPane.WARNING_MESSAGE);
    } finally {
        setCursor(Cursor.getDefaultCursor());
    }

}
 
Example 6
Project: HueSense   File: LanComm.java   Source Code and License Vote up 6 votes
private int pingDevice(NetworkDevice dev) throws IOException {

        InetAddress adr;
        if (dev.getHostname() != null) {
            adr = InetAddress.getByName(dev.getHostname());
        } else {
            adr = InetAddress.getByName(dev.getIp());
        }
        long start = System.currentTimeMillis();
        if (adr.isReachable(TIMEOUT_PING)) {
            long time = System.currentTimeMillis() - start;
            if (time == 0) {
                // special case, make sure that 0 is not returned when reachable
                return 1;
            }
            return (int) time;
        }
        return 0;

    }
 
Example 7
Project: openhab2-addon-hs110   File: HS110Handler.java   Source Code and License Vote up 6 votes
@Override
public Collection<ConfigStatusMessage> getConfigStatus() {

    Collection<ConfigStatusMessage> configStatus = new ArrayList<>();

    try {
        InetAddress ip = InetAddress.getByName(plug.ip);
        if (!ip.isReachable(500)) {
            configStatus.add(
                    ConfigStatusMessage.Builder.error("offline").withMessageKeySuffix("ip unreachable").build());
            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR);
        }
    } catch (IOException e) {
        logger.debug("Communication error ocurred reaching the device ", e);
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
    }

    return configStatus;
}
 
Example 8
Project: openjdk-jdk10   File: PingThis.java   Source Code and License Vote up 6 votes
public static void main(String args[]) throws Exception {
    if (System.getProperty("os.name").startsWith("Windows")) {
        return;
    }

    boolean preferIPv4Stack = "true".equals(System
            .getProperty("java.net.preferIPv4Stack"));
    List<String> addrs = new ArrayList<String>();
    InetAddress inetAddress = null;

    addrs.add("0.0.0.0");
    if (!preferIPv4Stack) {
        if (hasIPv6()) {
            addrs.add("::0");
        }
    }

    for (String addr : addrs) {
        inetAddress = InetAddress.getByName(addr);
        System.out.println("The target ip is "
                + inetAddress.getHostAddress());
        boolean isReachable = inetAddress.isReachable(3000);
        System.out.println("the target is reachable: " + isReachable);
        if (isReachable) {
            System.out.println("Test passed ");
        } else {
            System.out.println("Test failed ");
            throw new Exception("address " + inetAddress.getHostAddress()
                    + " can not be reachable!");
        }
    }
}
 
Example 9
Project: highfrequencyrobots   File: UDPSender.java   Source Code and License Vote up 6 votes
@Override
protected void establishConnection(InetAddress... addr) {
  for(InetAddress address : addr) {
    if(address != null) {
      try {
        boolean reachable = address.isReachable(2000);
        if(reachable) {
          establishConnection(address);
          break;
        }
      } catch (IOException e) {
        mLog.exception(e);
      }
    }
  }
}
 
Example 10
Project: Minecordbot   File: MCPing.java   Source Code and License Vote up 6 votes
/**
 * pings an ip address
 *
 * @param ipToPingNowPleaseGiveThisWhyIsThisFieldDescriptorSoLongLolEksDee idk lol
 * @return the ping
 */
public static String getPingToIP(String ipToPingNowPleaseGiveThisWhyIsThisFieldDescriptorSoLongLolEksDee) {
    try {
        InetAddress inet = InetAddress.getByName(ipToPingNowPleaseGiveThisWhyIsThisFieldDescriptorSoLongLolEksDee);

        long finish = 0;
        long start = new GregorianCalendar().getTimeInMillis();

        if (inet.isReachable(2500)) {
            finish = new GregorianCalendar().getTimeInMillis();
            return finish - start + " ms";
        } else {
            return "-1 ms";
        }
    } catch (Exception e) {
        //System.out.println("Exception:" + e.getMessage());
    }

    return "-1 ms";
}
 
Example 11
Project: jdk8u-jdk   File: PingThis.java   Source Code and License Vote up 5 votes
public static void main(String args[]) throws Exception {
    if (System.getProperty("os.name").startsWith("Windows")) {
        return;
    }

    boolean preferIPv4Stack = "true".equals(System
            .getProperty("java.net.preferIPv4Stack"));
    List<String> addrs = new ArrayList<String>();
    InetAddress inetAddress = null;

    addrs.add("0.0.0.0");
    if (!preferIPv4Stack) {
        if (hasIPv6()) {
            addrs.add("::0");
        }
    }

    for (String addr : addrs) {
        inetAddress = InetAddress.getByName(addr);
        System.out.println("The target ip is "
                + inetAddress.getHostAddress());
        boolean isReachable = inetAddress.isReachable(3000);
        System.out.println("the target is reachable: " + isReachable);
        if (isReachable) {
            System.out.println("Test passed ");
        } else {
            System.out.println("Test failed ");
            throw new Exception("address " + inetAddress.getHostAddress()
                    + " can not be reachable!");
        }
    }
}
 
Example 12
Project: jdk8u-jdk   File: IsReachable.java   Source Code and License Vote up 5 votes
public static void main(String[] args) {
    try {
        InetAddress addr = InetAddress.getByName("localhost");
        if (!addr.isReachable(10000))
            throw new RuntimeException("Localhost should always be reachable");
        NetworkInterface inf = NetworkInterface.getByInetAddress(addr);
        if (inf != null) {
            if (!addr.isReachable(inf, 20, 10000))
            throw new RuntimeException("Localhost should always be reachable");
        }

    } catch (IOException e) {
        throw new RuntimeException("Unexpected exception:" + e);
    }
}
 
Example 13
Project: openhab2-addon-hs110   File: HS110.java   Source Code and License Vote up 5 votes
public boolean isPresent() {

        try {

            InetAddress ip = InetAddress.getByName(this.ip);
            return ip.isReachable(500);
        } catch (IOException ignore) {
            // ignore this failing and return false
        }
        return false;
    }
 
Example 14
Project: uavstack   File: NetworkHelper.java   Source Code and License Vote up 5 votes
public static boolean isIPReachable(String ipStr, int timeout) {

        try {
            InetAddress inetAddress = InetAddress.getByName(ipStr);

            if (inetAddress.isReachable(timeout)) {
                return true;
            }
        }
        catch (Exception e) {
            // ignore
        }

        return false;
    }
 
Example 15
Project: openjdk-jdk10   File: IsReachable.java   Source Code and License Vote up 5 votes
public static void main(String[] args) {
    try {
        InetAddress addr = InetAddress.getByName("localhost");
        if (!addr.isReachable(10000))
            throw new RuntimeException("Localhost should always be reachable");
        NetworkInterface inf = NetworkInterface.getByInetAddress(addr);
        if (inf != null) {
            if (!addr.isReachable(inf, 20, 10000))
            throw new RuntimeException("Localhost should always be reachable");
        }

    } catch (IOException e) {
        throw new RuntimeException("Unexpected exception:" + e);
    }
}