Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Host Reachability/Availability in Android

Tags:

android

Ive been trying to do what i thought would be a simple reachability of host test at the beginning of my apps internet ventures, but documentation isnt helping and neither are examples found at various places, ive tried many solutions with no luck, so if anyone could point me in the direction of a definitive way to check a hosts availability with android that be awesome, just need it to toggle a bool to true if the host can be reached

im using API8 if that makes much difference to this process, and must cater for non-rooted devices so the inetaddress.isReachable is out

like image 793
fury-s12 Avatar asked Jan 18 '12 23:01

fury-s12


People also ask

How do I know if my URL is working android?

you can use the follow code to try. final String customURL = "http://www.desicomments.com/dc3/08/273858/273858.jpg"; new Thread(){ @Override public void run() { // TODO Auto-generated method stub super. run(); try { URL url = new URL(customURL); HttpURLConnection con = (HttpURLConnection) url. openConnection(); con.

How do I check my kotlin internet connection?

Refer to How to Create/Start a New Project in Android Studio, to know how to create an empty activity project. And select Kotlin as the programming language. The main layout of the application contains only one button. Which upon clicking, a toast appears which contains the status of the connectivity.


3 Answers

It's not pretty but this is how I did it:

boolean exists = false;  try {     SocketAddress sockaddr = new InetSocketAddress(ip, port);     // Create an unbound socket     Socket sock = new Socket();      // This method will block no more than timeoutMs.     // If the timeout occurs, SocketTimeoutException is thrown.     int timeoutMs = 2000;   // 2 seconds     sock.connect(sockaddr, timeoutMs);     exists = true; } catch(IOException e) {     // Handle exception } 
like image 182
ghostbust555 Avatar answered Sep 24 '22 18:09

ghostbust555


To check connectivity you could use:

public boolean isOnline(Context context) {      ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);         NetworkInfo netInfo = cm.getActiveNetworkInfo();         return netInfo != null && netInfo.isConnectedOrConnecting(); } 

If it reports a connection, you could also then check via trying to do a http get to an address and then checking the status code that is returned. if no status code is returned it's pretty certain the host is unreachable.

like image 30
Richard Lewin Avatar answered Sep 25 '22 18:09

Richard Lewin


A Faster Solution

Instead of opening a complete socket connection you could use inetAddress.isReachable(int timeout). That would make the check faster but also more imprecise because this method just builds upon an echo request:

A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

For my use case I had to establish a connection to a web server. Therefore it was necessary to me that the service on the server was up and running. So a socket connection was my preferred choice over a simple echo request.


Standard Solution

Java 7 and above

That's the code that I'm using for any Java 7 and above project:

/**
 * Check if host is reachable.
 * @param host The host to check for availability. Can either be a machine name, such as "google.com",
 *             or a textual representation of its IP address, such as "8.8.8.8".
 * @param port The port number.
 * @param timeout The timeout in milliseconds.
 * @return True if the host is reachable. False otherwise.
 */
public static boolean isHostAvailable(final String host, final int port, final int timeout) {
    try (final Socket socket = new Socket()) {
        final InetAddress inetAddress = InetAddress.getByName(host);
        final InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, port);

        socket.connect(inetSocketAddress, timeout);
        return true;
    } catch (java.io.IOException e) {
        e.printStackTrace();
        return false;
    }
}

Below Java 7

The try-catch-resource block from the code above works only with Java 7 and a newer version. Prior to version 7 a finally-block is needed to ensure the resource is closed correctly:

public static boolean isHostAvailable(final String host, final int port, final int timeout) {
    final Socket socket = new Socket();
    try {
        ... // same as above
    } catch (java.io.IOException e) {
        ... // same as above
    } finally {
        if (socket != null) {
            socket.close(); // this will throw another exception... just let the function throw it
        }
    }
}

Usage

host can either be a machine name, such as "google.com", or an IP address, such as "8.8.8.8".

if (isHostAvailable("google.com", 80, 1000)) {
    // do you work here
}

Further reading

Android Docs:

  • Socket
  • InetSocketAddress
  • InetAddress.getByName()
  • InetAddress.isReachable()

Stackoverflow:

  • Preferred Java way to ping an HTTP URL for availability
like image 42
winklerrr Avatar answered Sep 24 '22 18:09

winklerrr