I want to check if server is reachable. Found an example here to get server's IP as integer value. Im trying to check it in following way (in activity):
boolean s = isHostReachable(getString(R.string.server_address));
but no luck, it returns false, while
final int serverIP = getHostIPAsInt(getString(R.string.server_address));
always returns a value. Here are the methods:
public static boolean isHostReachable(final String hostName){
try {
return InetAddress.getByName(hostName).isReachable(255);
} catch (UnknownHostException e) {
return false;
} catch (IOException e){
return false;
}
}
public static int getHostIPAsInt(final String hostname) {
final InetAddress inetAddress = getHostIP(hostname);
if (inetAddress==null)
return 0;
final byte[] addrBytes = inetAddress.getAddress();
final int addr = (addrBytes[3]&0xff) << 24
| (addrBytes[2]&0xff) << 16
| (addrBytes[1]&0xff) << 8
| addrBytes[0]&0xff;
return addr;
also i tried the following with same result:
isInternetWiFi = cm.requestRouteToHost(ConnectivityManager.TYPE_WIFI, serverIP);
isInternetMobile = cm.requestRouteToHost(ConnectivityManager.TYPE_MOBILE, serverIP);
both are false even if i use "google.com". I got a feeling when serverIP is a negative value it always fails... But InetAddress doesn't use the serverIP and still returns false.
trying it with emulator (aos 2.2).
i need to ensure the server is up to continue executing app due 2 app's functionality depends on server (client/server app). But the idea was to check if the device uses (connected via) WiFi which has no real access to the internet like some local area WiFi. So i need to check 1st if server is reachable at all and the second - if it responds. How2do it?
以上就是an issue while trying to ensure the server is accesible (HostReachable)的详细内容,更多请关注web前端其它相关文章!