當前位置: 首頁>>代碼示例>>Java>>正文


Java InetAddress.isReachable方法代碼示例

本文整理匯總了Java中java.net.InetAddress.isReachable方法的典型用法代碼示例。如果您正苦於以下問題:Java InetAddress.isReachable方法的具體用法?Java InetAddress.isReachable怎麽用?Java InetAddress.isReachable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.InetAddress的用法示例。


在下文中一共展示了InetAddress.isReachable方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doInBackground

import java.net.InetAddress; //導入方法依賴的package包/類
@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;
}
 
開發者ID:MBach,項目名稱:home-automation,代碼行數:24,代碼來源:AsyncNetworkRequest.java

示例2: isValid

import java.net.InetAddress; //導入方法依賴的package包/類
@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;
}
 
開發者ID:hekate-io,項目名稱:hekate,代碼行數:23,代碼來源:HostReachabilityDetector.java

示例3: isReachable

import java.net.InetAddress; //導入方法依賴的package包/類
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;
}
 
開發者ID:comtel2000,項目名稱:mokka7,代碼行數:19,代碼來源:PingWatchdogService.java

示例4: pingButtonActionPerformed

import java.net.InetAddress; //導入方法依賴的package包/類
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());
    }

}
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:19,代碼來源:SpiNNaker_InterfaceFactory.java

示例5: pingButtonActionPerformed

import java.net.InetAddress; //導入方法依賴的package包/類
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());
    }

}
 
開發者ID:SensorsINI,項目名稱:jaer,代碼行數:19,代碼來源:eDVS128_InterfaceFactory.java

示例6: pingDevice

import java.net.InetAddress; //導入方法依賴的package包/類
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;

    }
 
開發者ID:dainesch,項目名稱:HueSense,代碼行數:21,代碼來源:LanComm.java

示例7: getConfigStatus

import java.net.InetAddress; //導入方法依賴的package包/類
@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;
}
 
開發者ID:computerlyrik,項目名稱:openhab2-addon-hs110,代碼行數:20,代碼來源:HS110Handler.java

示例8: main

import java.net.InetAddress; //導入方法依賴的package包/類
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!");
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:33,代碼來源:PingThis.java

示例9: establishConnection

import java.net.InetAddress; //導入方法依賴的package包/類
@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);
      }
    }
  }
}
 
開發者ID:flybotix,項目名稱:highfrequencyrobots,代碼行數:17,代碼來源:UDPSender.java

示例10: getPingToIP

import java.net.InetAddress; //導入方法依賴的package包/類
/**
 * 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";
}
 
開發者ID:CyR1en,項目名稱:Minecordbot,代碼行數:26,代碼來源:MCPing.java

示例11: main

import java.net.InetAddress; //導入方法依賴的package包/類
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);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:16,代碼來源:IsReachable.java

示例12: isPresent

import java.net.InetAddress; //導入方法依賴的package包/類
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;
    }
 
開發者ID:computerlyrik,項目名稱:openhab2-addon-hs110,代碼行數:12,代碼來源:HS110.java

示例13: isIPReachable

import java.net.InetAddress; //導入方法依賴的package包/類
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;
    }
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:16,代碼來源:NetworkHelper.java


注:本文中的java.net.InetAddress.isReachable方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。