本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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());
}
}
示例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());
}
}
示例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;
}
示例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;
}
示例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!");
}
}
}
示例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);
}
}
}
}
示例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";
}
示例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);
}
}
示例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;
}
示例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;
}