java - best Alternative for InetAddress.getByName(host).isReachable(timeout) -


i trying reach host , have following code

if(!inetaddress.getbyname(host).isreachable(timeout)){    throw new exception("host not exist::"+ hostname);  } 

the hostname able ping windows, , did tracert on , returns packets. java throws out exception "host not exist::";

the timeout value experimented giving 2000ms, 5000ms. tried 3000 well. cause of problem not able understand. researched on net , inetaddress.getbyname(host).isreachable(time) not reliable , behaves according internal system.

what best alternative if true. please suggest.

either open tcp socket port think open (22 linux, 139 windows, etc.)

public static boolean isreachablebytcp(string host, int port, int timeout) {     try {         socket socket = new socket();         socketaddress socketaddress = new inetsocketaddress(host, port);         socket.connect(socketaddress, timeout);         socket.close();         return true;     } catch (ioexception e) {         return false;     } } 

or use hack send actual ping. (inspired here: http://www.inprose.com/en/content/icmp-ping-in-java)

public static boolean isreachablebyping(string host) {     try{         string cmd = "";          if(system.getproperty("os.name").startswith("windows"))             cmd = "cmd /c ping -n 1 " + host + " | find \"ttl\"";         else             cmd = "ping -c 1 " + host;          process myprocess = runtime.getruntime().exec(cmd);         myprocess.waitfor();          return myprocess.exitvalue() == 0;     } catch( exception e ) {         e.printstacktrace();         return false;     } } 

same hack android can found here:


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -