how to icmp ping on android












15















I need to do a icmp ping to a host from my android device. I need to measure the round trip time. I am proficient with android and java, just dont know what library to use.
How do I do it?
Is it possible via 3G, Edge?










share|improve this question























  • Why do you need to measure the ICMP round trip time?

    – dbasnett
    Nov 23 '11 at 12:52
















15















I need to do a icmp ping to a host from my android device. I need to measure the round trip time. I am proficient with android and java, just dont know what library to use.
How do I do it?
Is it possible via 3G, Edge?










share|improve this question























  • Why do you need to measure the ICMP round trip time?

    – dbasnett
    Nov 23 '11 at 12:52














15












15








15


10






I need to do a icmp ping to a host from my android device. I need to measure the round trip time. I am proficient with android and java, just dont know what library to use.
How do I do it?
Is it possible via 3G, Edge?










share|improve this question














I need to do a icmp ping to a host from my android device. I need to measure the round trip time. I am proficient with android and java, just dont know what library to use.
How do I do it?
Is it possible via 3G, Edge?







android icmp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 16 '11 at 23:02









AbhishekAbhishek

73362037




73362037













  • Why do you need to measure the ICMP round trip time?

    – dbasnett
    Nov 23 '11 at 12:52



















  • Why do you need to measure the ICMP round trip time?

    – dbasnett
    Nov 23 '11 at 12:52

















Why do you need to measure the ICMP round trip time?

– dbasnett
Nov 23 '11 at 12:52





Why do you need to measure the ICMP round trip time?

– dbasnett
Nov 23 '11 at 12:52












4 Answers
4






active

oldest

votes


















15














Yes you can ping with 3G, edge, wireless whatever, as long as you have connectivity. The only limitation is in the emulator, see here:
http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297



Here is my ping function:



package com.namespace.router.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.util.Log;

public class Network {

private static final String TAG = "Network.java";

public static String pingError = null;

/**
* Ping a host and return an int value of 0 or 1 or 2 0=success, 1=fail, 2=error
*
* Does not work in Android emulator and also delay by '1' second if host not pingable
* In the Android emulator only ping to 127.0.0.1 works
*
* @param String host in dotted IP address format
* @return
* @throws IOException
* @throws InterruptedException
*/
public static int pingHost(String host) throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("ping -c 1 " + host);
proc.waitFor();
int exit = proc.exitValue();
return exit;
}

public static String ping(String host) throws IOException, InterruptedException {
StringBuffer echo = new StringBuffer();
Runtime runtime = Runtime.getRuntime();
Log.v(TAG, "About to ping using runtime.exec");
Process proc = runtime.exec("ping -c 1 " + host);
proc.waitFor();
int exit = proc.exitValue();
if (exit == 0) {
InputStreamReader reader = new InputStreamReader(proc.getInputStream());
BufferedReader buffer = new BufferedReader(reader);
String line = "";
while ((line = buffer.readLine()) != null) {
echo.append(line + "n");
}
return getPingStats(echo.toString());
} else if (exit == 1) {
pingError = "failed, exit = 1";
return null;
} else {
pingError = "error, exit = 2";
return null;
}
}

/**
* getPingStats interprets the text result of a Linux ping command
*
* Set pingError on error and return null
*
* http://en.wikipedia.org/wiki/Ping
*
* PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
* 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.251 ms
* 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.294 ms
* 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.295 ms
* 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.300 ms
*
* --- 127.0.0.1 ping statistics ---
* 4 packets transmitted, 4 received, 0% packet loss, time 0ms
* rtt min/avg/max/mdev = 0.251/0.285/0.300/0.019 ms
*
* PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
*
* --- 192.168.0.2 ping statistics ---
* 1 packets transmitted, 0 received, 100% packet loss, time 0ms
*
* # ping 321321.
* ping: unknown host 321321.
*
* 1. Check if output contains 0% packet loss : Branch to success -> Get stats
* 2. Check if output contains 100% packet loss : Branch to fail -> No stats
* 3. Check if output contains 25% packet loss : Branch to partial success -> Get stats
* 4. Check if output contains "unknown host"
*
* @param s
*/
public static String getPingStats(String s) {
if (s.contains("0% packet loss")) {
int start = s.indexOf("/mdev = ");
int end = s.indexOf(" msn", start);
s = s.substring(start + 8, end);
String stats = s.split("/");
return stats[2];
} else if (s.contains("100% packet loss")) {
pingError = "100% packet loss";
return null;
} else if (s.contains("% packet loss")) {
pingError = "partial packet loss";
return null;
} else if (s.contains("unknown host")) {
pingError = "unknown host";
return null;
} else {
pingError = "unknown error in getPingStats";
return null;
}
}
}





share|improve this answer



















  • 1





    ping(8.8.8.8) always fail

    – danarj
    Nov 9 '14 at 11:32











  • Not working on samsung s3 4.2.2 and other 4.2.2 device !!

    – Punit Sharma
    Feb 10 '16 at 10:28











  • take care with the string matching: .contains "100% packet loss" and "0% packet loss" are the same.

    – Callum Wilson
    Jun 19 '17 at 20:57











  • emulator is not the only limit, there are a lot of devices that don't support ping command

    – Shayan_Aryan
    Jun 19 '18 at 9:29



















6














You will probably want to use the isReachable - see more details in the Android doc. However, apparently some networks block ICMP. There is a post where you can read more about this issue here.






share|improve this answer

































    2














    you can use the open source code of terminal emulator available here



    build the library(using cygwin and android-ndk) file and then use






    share|improve this answer































      -1














      From the socket(2) man page ping access in the device is restricted by the content of the /proc/sys/net/ipv4/ping_group_range file



      $ cat /proc/sys/net/ipv4/ping_group_range



      It is "1 0" by default, meaning
      that nobody (not even root) may create ping sockets. Setting it to "100
      100" would grant permissions to the single group (to either make
      /sbin/ping g+s and owned by this group or to grant permissions to the
      "netadmins" group), "0 4294967295" would enable it for the world, "100
      4294967295" would enable it for the users, but not daemons.



      so any devices with other than "0 4294967295" cant access from an android java application



      In emulator you can test this be resetting



      sysctl -w net.ipv4.ping_group_range="0 0" // to some range






      share|improve this answer
























      • ...and this answers the original question how?

        – Stefan Becker
        Jan 19 at 6:22











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7451200%2fhow-to-icmp-ping-on-android%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      15














      Yes you can ping with 3G, edge, wireless whatever, as long as you have connectivity. The only limitation is in the emulator, see here:
      http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297



      Here is my ping function:



      package com.namespace.router.api;

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;

      import android.util.Log;

      public class Network {

      private static final String TAG = "Network.java";

      public static String pingError = null;

      /**
      * Ping a host and return an int value of 0 or 1 or 2 0=success, 1=fail, 2=error
      *
      * Does not work in Android emulator and also delay by '1' second if host not pingable
      * In the Android emulator only ping to 127.0.0.1 works
      *
      * @param String host in dotted IP address format
      * @return
      * @throws IOException
      * @throws InterruptedException
      */
      public static int pingHost(String host) throws IOException, InterruptedException {
      Runtime runtime = Runtime.getRuntime();
      Process proc = runtime.exec("ping -c 1 " + host);
      proc.waitFor();
      int exit = proc.exitValue();
      return exit;
      }

      public static String ping(String host) throws IOException, InterruptedException {
      StringBuffer echo = new StringBuffer();
      Runtime runtime = Runtime.getRuntime();
      Log.v(TAG, "About to ping using runtime.exec");
      Process proc = runtime.exec("ping -c 1 " + host);
      proc.waitFor();
      int exit = proc.exitValue();
      if (exit == 0) {
      InputStreamReader reader = new InputStreamReader(proc.getInputStream());
      BufferedReader buffer = new BufferedReader(reader);
      String line = "";
      while ((line = buffer.readLine()) != null) {
      echo.append(line + "n");
      }
      return getPingStats(echo.toString());
      } else if (exit == 1) {
      pingError = "failed, exit = 1";
      return null;
      } else {
      pingError = "error, exit = 2";
      return null;
      }
      }

      /**
      * getPingStats interprets the text result of a Linux ping command
      *
      * Set pingError on error and return null
      *
      * http://en.wikipedia.org/wiki/Ping
      *
      * PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
      * 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.251 ms
      * 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.294 ms
      * 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.295 ms
      * 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.300 ms
      *
      * --- 127.0.0.1 ping statistics ---
      * 4 packets transmitted, 4 received, 0% packet loss, time 0ms
      * rtt min/avg/max/mdev = 0.251/0.285/0.300/0.019 ms
      *
      * PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
      *
      * --- 192.168.0.2 ping statistics ---
      * 1 packets transmitted, 0 received, 100% packet loss, time 0ms
      *
      * # ping 321321.
      * ping: unknown host 321321.
      *
      * 1. Check if output contains 0% packet loss : Branch to success -> Get stats
      * 2. Check if output contains 100% packet loss : Branch to fail -> No stats
      * 3. Check if output contains 25% packet loss : Branch to partial success -> Get stats
      * 4. Check if output contains "unknown host"
      *
      * @param s
      */
      public static String getPingStats(String s) {
      if (s.contains("0% packet loss")) {
      int start = s.indexOf("/mdev = ");
      int end = s.indexOf(" msn", start);
      s = s.substring(start + 8, end);
      String stats = s.split("/");
      return stats[2];
      } else if (s.contains("100% packet loss")) {
      pingError = "100% packet loss";
      return null;
      } else if (s.contains("% packet loss")) {
      pingError = "partial packet loss";
      return null;
      } else if (s.contains("unknown host")) {
      pingError = "unknown host";
      return null;
      } else {
      pingError = "unknown error in getPingStats";
      return null;
      }
      }
      }





      share|improve this answer



















      • 1





        ping(8.8.8.8) always fail

        – danarj
        Nov 9 '14 at 11:32











      • Not working on samsung s3 4.2.2 and other 4.2.2 device !!

        – Punit Sharma
        Feb 10 '16 at 10:28











      • take care with the string matching: .contains "100% packet loss" and "0% packet loss" are the same.

        – Callum Wilson
        Jun 19 '17 at 20:57











      • emulator is not the only limit, there are a lot of devices that don't support ping command

        – Shayan_Aryan
        Jun 19 '18 at 9:29
















      15














      Yes you can ping with 3G, edge, wireless whatever, as long as you have connectivity. The only limitation is in the emulator, see here:
      http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297



      Here is my ping function:



      package com.namespace.router.api;

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;

      import android.util.Log;

      public class Network {

      private static final String TAG = "Network.java";

      public static String pingError = null;

      /**
      * Ping a host and return an int value of 0 or 1 or 2 0=success, 1=fail, 2=error
      *
      * Does not work in Android emulator and also delay by '1' second if host not pingable
      * In the Android emulator only ping to 127.0.0.1 works
      *
      * @param String host in dotted IP address format
      * @return
      * @throws IOException
      * @throws InterruptedException
      */
      public static int pingHost(String host) throws IOException, InterruptedException {
      Runtime runtime = Runtime.getRuntime();
      Process proc = runtime.exec("ping -c 1 " + host);
      proc.waitFor();
      int exit = proc.exitValue();
      return exit;
      }

      public static String ping(String host) throws IOException, InterruptedException {
      StringBuffer echo = new StringBuffer();
      Runtime runtime = Runtime.getRuntime();
      Log.v(TAG, "About to ping using runtime.exec");
      Process proc = runtime.exec("ping -c 1 " + host);
      proc.waitFor();
      int exit = proc.exitValue();
      if (exit == 0) {
      InputStreamReader reader = new InputStreamReader(proc.getInputStream());
      BufferedReader buffer = new BufferedReader(reader);
      String line = "";
      while ((line = buffer.readLine()) != null) {
      echo.append(line + "n");
      }
      return getPingStats(echo.toString());
      } else if (exit == 1) {
      pingError = "failed, exit = 1";
      return null;
      } else {
      pingError = "error, exit = 2";
      return null;
      }
      }

      /**
      * getPingStats interprets the text result of a Linux ping command
      *
      * Set pingError on error and return null
      *
      * http://en.wikipedia.org/wiki/Ping
      *
      * PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
      * 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.251 ms
      * 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.294 ms
      * 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.295 ms
      * 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.300 ms
      *
      * --- 127.0.0.1 ping statistics ---
      * 4 packets transmitted, 4 received, 0% packet loss, time 0ms
      * rtt min/avg/max/mdev = 0.251/0.285/0.300/0.019 ms
      *
      * PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
      *
      * --- 192.168.0.2 ping statistics ---
      * 1 packets transmitted, 0 received, 100% packet loss, time 0ms
      *
      * # ping 321321.
      * ping: unknown host 321321.
      *
      * 1. Check if output contains 0% packet loss : Branch to success -> Get stats
      * 2. Check if output contains 100% packet loss : Branch to fail -> No stats
      * 3. Check if output contains 25% packet loss : Branch to partial success -> Get stats
      * 4. Check if output contains "unknown host"
      *
      * @param s
      */
      public static String getPingStats(String s) {
      if (s.contains("0% packet loss")) {
      int start = s.indexOf("/mdev = ");
      int end = s.indexOf(" msn", start);
      s = s.substring(start + 8, end);
      String stats = s.split("/");
      return stats[2];
      } else if (s.contains("100% packet loss")) {
      pingError = "100% packet loss";
      return null;
      } else if (s.contains("% packet loss")) {
      pingError = "partial packet loss";
      return null;
      } else if (s.contains("unknown host")) {
      pingError = "unknown host";
      return null;
      } else {
      pingError = "unknown error in getPingStats";
      return null;
      }
      }
      }





      share|improve this answer



















      • 1





        ping(8.8.8.8) always fail

        – danarj
        Nov 9 '14 at 11:32











      • Not working on samsung s3 4.2.2 and other 4.2.2 device !!

        – Punit Sharma
        Feb 10 '16 at 10:28











      • take care with the string matching: .contains "100% packet loss" and "0% packet loss" are the same.

        – Callum Wilson
        Jun 19 '17 at 20:57











      • emulator is not the only limit, there are a lot of devices that don't support ping command

        – Shayan_Aryan
        Jun 19 '18 at 9:29














      15












      15








      15







      Yes you can ping with 3G, edge, wireless whatever, as long as you have connectivity. The only limitation is in the emulator, see here:
      http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297



      Here is my ping function:



      package com.namespace.router.api;

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;

      import android.util.Log;

      public class Network {

      private static final String TAG = "Network.java";

      public static String pingError = null;

      /**
      * Ping a host and return an int value of 0 or 1 or 2 0=success, 1=fail, 2=error
      *
      * Does not work in Android emulator and also delay by '1' second if host not pingable
      * In the Android emulator only ping to 127.0.0.1 works
      *
      * @param String host in dotted IP address format
      * @return
      * @throws IOException
      * @throws InterruptedException
      */
      public static int pingHost(String host) throws IOException, InterruptedException {
      Runtime runtime = Runtime.getRuntime();
      Process proc = runtime.exec("ping -c 1 " + host);
      proc.waitFor();
      int exit = proc.exitValue();
      return exit;
      }

      public static String ping(String host) throws IOException, InterruptedException {
      StringBuffer echo = new StringBuffer();
      Runtime runtime = Runtime.getRuntime();
      Log.v(TAG, "About to ping using runtime.exec");
      Process proc = runtime.exec("ping -c 1 " + host);
      proc.waitFor();
      int exit = proc.exitValue();
      if (exit == 0) {
      InputStreamReader reader = new InputStreamReader(proc.getInputStream());
      BufferedReader buffer = new BufferedReader(reader);
      String line = "";
      while ((line = buffer.readLine()) != null) {
      echo.append(line + "n");
      }
      return getPingStats(echo.toString());
      } else if (exit == 1) {
      pingError = "failed, exit = 1";
      return null;
      } else {
      pingError = "error, exit = 2";
      return null;
      }
      }

      /**
      * getPingStats interprets the text result of a Linux ping command
      *
      * Set pingError on error and return null
      *
      * http://en.wikipedia.org/wiki/Ping
      *
      * PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
      * 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.251 ms
      * 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.294 ms
      * 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.295 ms
      * 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.300 ms
      *
      * --- 127.0.0.1 ping statistics ---
      * 4 packets transmitted, 4 received, 0% packet loss, time 0ms
      * rtt min/avg/max/mdev = 0.251/0.285/0.300/0.019 ms
      *
      * PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
      *
      * --- 192.168.0.2 ping statistics ---
      * 1 packets transmitted, 0 received, 100% packet loss, time 0ms
      *
      * # ping 321321.
      * ping: unknown host 321321.
      *
      * 1. Check if output contains 0% packet loss : Branch to success -> Get stats
      * 2. Check if output contains 100% packet loss : Branch to fail -> No stats
      * 3. Check if output contains 25% packet loss : Branch to partial success -> Get stats
      * 4. Check if output contains "unknown host"
      *
      * @param s
      */
      public static String getPingStats(String s) {
      if (s.contains("0% packet loss")) {
      int start = s.indexOf("/mdev = ");
      int end = s.indexOf(" msn", start);
      s = s.substring(start + 8, end);
      String stats = s.split("/");
      return stats[2];
      } else if (s.contains("100% packet loss")) {
      pingError = "100% packet loss";
      return null;
      } else if (s.contains("% packet loss")) {
      pingError = "partial packet loss";
      return null;
      } else if (s.contains("unknown host")) {
      pingError = "unknown host";
      return null;
      } else {
      pingError = "unknown error in getPingStats";
      return null;
      }
      }
      }





      share|improve this answer













      Yes you can ping with 3G, edge, wireless whatever, as long as you have connectivity. The only limitation is in the emulator, see here:
      http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297



      Here is my ping function:



      package com.namespace.router.api;

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;

      import android.util.Log;

      public class Network {

      private static final String TAG = "Network.java";

      public static String pingError = null;

      /**
      * Ping a host and return an int value of 0 or 1 or 2 0=success, 1=fail, 2=error
      *
      * Does not work in Android emulator and also delay by '1' second if host not pingable
      * In the Android emulator only ping to 127.0.0.1 works
      *
      * @param String host in dotted IP address format
      * @return
      * @throws IOException
      * @throws InterruptedException
      */
      public static int pingHost(String host) throws IOException, InterruptedException {
      Runtime runtime = Runtime.getRuntime();
      Process proc = runtime.exec("ping -c 1 " + host);
      proc.waitFor();
      int exit = proc.exitValue();
      return exit;
      }

      public static String ping(String host) throws IOException, InterruptedException {
      StringBuffer echo = new StringBuffer();
      Runtime runtime = Runtime.getRuntime();
      Log.v(TAG, "About to ping using runtime.exec");
      Process proc = runtime.exec("ping -c 1 " + host);
      proc.waitFor();
      int exit = proc.exitValue();
      if (exit == 0) {
      InputStreamReader reader = new InputStreamReader(proc.getInputStream());
      BufferedReader buffer = new BufferedReader(reader);
      String line = "";
      while ((line = buffer.readLine()) != null) {
      echo.append(line + "n");
      }
      return getPingStats(echo.toString());
      } else if (exit == 1) {
      pingError = "failed, exit = 1";
      return null;
      } else {
      pingError = "error, exit = 2";
      return null;
      }
      }

      /**
      * getPingStats interprets the text result of a Linux ping command
      *
      * Set pingError on error and return null
      *
      * http://en.wikipedia.org/wiki/Ping
      *
      * PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
      * 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.251 ms
      * 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.294 ms
      * 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.295 ms
      * 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.300 ms
      *
      * --- 127.0.0.1 ping statistics ---
      * 4 packets transmitted, 4 received, 0% packet loss, time 0ms
      * rtt min/avg/max/mdev = 0.251/0.285/0.300/0.019 ms
      *
      * PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
      *
      * --- 192.168.0.2 ping statistics ---
      * 1 packets transmitted, 0 received, 100% packet loss, time 0ms
      *
      * # ping 321321.
      * ping: unknown host 321321.
      *
      * 1. Check if output contains 0% packet loss : Branch to success -> Get stats
      * 2. Check if output contains 100% packet loss : Branch to fail -> No stats
      * 3. Check if output contains 25% packet loss : Branch to partial success -> Get stats
      * 4. Check if output contains "unknown host"
      *
      * @param s
      */
      public static String getPingStats(String s) {
      if (s.contains("0% packet loss")) {
      int start = s.indexOf("/mdev = ");
      int end = s.indexOf(" msn", start);
      s = s.substring(start + 8, end);
      String stats = s.split("/");
      return stats[2];
      } else if (s.contains("100% packet loss")) {
      pingError = "100% packet loss";
      return null;
      } else if (s.contains("% packet loss")) {
      pingError = "partial packet loss";
      return null;
      } else if (s.contains("unknown host")) {
      pingError = "unknown host";
      return null;
      } else {
      pingError = "unknown error in getPingStats";
      return null;
      }
      }
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 3 '12 at 10:16









      Eugene van der MerweEugene van der Merwe

      2,73312342




      2,73312342








      • 1





        ping(8.8.8.8) always fail

        – danarj
        Nov 9 '14 at 11:32











      • Not working on samsung s3 4.2.2 and other 4.2.2 device !!

        – Punit Sharma
        Feb 10 '16 at 10:28











      • take care with the string matching: .contains "100% packet loss" and "0% packet loss" are the same.

        – Callum Wilson
        Jun 19 '17 at 20:57











      • emulator is not the only limit, there are a lot of devices that don't support ping command

        – Shayan_Aryan
        Jun 19 '18 at 9:29














      • 1





        ping(8.8.8.8) always fail

        – danarj
        Nov 9 '14 at 11:32











      • Not working on samsung s3 4.2.2 and other 4.2.2 device !!

        – Punit Sharma
        Feb 10 '16 at 10:28











      • take care with the string matching: .contains "100% packet loss" and "0% packet loss" are the same.

        – Callum Wilson
        Jun 19 '17 at 20:57











      • emulator is not the only limit, there are a lot of devices that don't support ping command

        – Shayan_Aryan
        Jun 19 '18 at 9:29








      1




      1





      ping(8.8.8.8) always fail

      – danarj
      Nov 9 '14 at 11:32





      ping(8.8.8.8) always fail

      – danarj
      Nov 9 '14 at 11:32













      Not working on samsung s3 4.2.2 and other 4.2.2 device !!

      – Punit Sharma
      Feb 10 '16 at 10:28





      Not working on samsung s3 4.2.2 and other 4.2.2 device !!

      – Punit Sharma
      Feb 10 '16 at 10:28













      take care with the string matching: .contains "100% packet loss" and "0% packet loss" are the same.

      – Callum Wilson
      Jun 19 '17 at 20:57





      take care with the string matching: .contains "100% packet loss" and "0% packet loss" are the same.

      – Callum Wilson
      Jun 19 '17 at 20:57













      emulator is not the only limit, there are a lot of devices that don't support ping command

      – Shayan_Aryan
      Jun 19 '18 at 9:29





      emulator is not the only limit, there are a lot of devices that don't support ping command

      – Shayan_Aryan
      Jun 19 '18 at 9:29













      6














      You will probably want to use the isReachable - see more details in the Android doc. However, apparently some networks block ICMP. There is a post where you can read more about this issue here.






      share|improve this answer






























        6














        You will probably want to use the isReachable - see more details in the Android doc. However, apparently some networks block ICMP. There is a post where you can read more about this issue here.






        share|improve this answer




























          6












          6








          6







          You will probably want to use the isReachable - see more details in the Android doc. However, apparently some networks block ICMP. There is a post where you can read more about this issue here.






          share|improve this answer















          You will probably want to use the isReachable - see more details in the Android doc. However, apparently some networks block ICMP. There is a post where you can read more about this issue here.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 23 '17 at 12:24









          Community

          11




          11










          answered Sep 16 '11 at 23:11









          AndreiAndrei

          2,42211824




          2,42211824























              2














              you can use the open source code of terminal emulator available here



              build the library(using cygwin and android-ndk) file and then use






              share|improve this answer




























                2














                you can use the open source code of terminal emulator available here



                build the library(using cygwin and android-ndk) file and then use






                share|improve this answer


























                  2












                  2








                  2







                  you can use the open source code of terminal emulator available here



                  build the library(using cygwin and android-ndk) file and then use






                  share|improve this answer













                  you can use the open source code of terminal emulator available here



                  build the library(using cygwin and android-ndk) file and then use







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 17 '11 at 4:12









                  Azhar ShaikhAzhar Shaikh

                  34.9k1280110




                  34.9k1280110























                      -1














                      From the socket(2) man page ping access in the device is restricted by the content of the /proc/sys/net/ipv4/ping_group_range file



                      $ cat /proc/sys/net/ipv4/ping_group_range



                      It is "1 0" by default, meaning
                      that nobody (not even root) may create ping sockets. Setting it to "100
                      100" would grant permissions to the single group (to either make
                      /sbin/ping g+s and owned by this group or to grant permissions to the
                      "netadmins" group), "0 4294967295" would enable it for the world, "100
                      4294967295" would enable it for the users, but not daemons.



                      so any devices with other than "0 4294967295" cant access from an android java application



                      In emulator you can test this be resetting



                      sysctl -w net.ipv4.ping_group_range="0 0" // to some range






                      share|improve this answer
























                      • ...and this answers the original question how?

                        – Stefan Becker
                        Jan 19 at 6:22
















                      -1














                      From the socket(2) man page ping access in the device is restricted by the content of the /proc/sys/net/ipv4/ping_group_range file



                      $ cat /proc/sys/net/ipv4/ping_group_range



                      It is "1 0" by default, meaning
                      that nobody (not even root) may create ping sockets. Setting it to "100
                      100" would grant permissions to the single group (to either make
                      /sbin/ping g+s and owned by this group or to grant permissions to the
                      "netadmins" group), "0 4294967295" would enable it for the world, "100
                      4294967295" would enable it for the users, but not daemons.



                      so any devices with other than "0 4294967295" cant access from an android java application



                      In emulator you can test this be resetting



                      sysctl -w net.ipv4.ping_group_range="0 0" // to some range






                      share|improve this answer
























                      • ...and this answers the original question how?

                        – Stefan Becker
                        Jan 19 at 6:22














                      -1












                      -1








                      -1







                      From the socket(2) man page ping access in the device is restricted by the content of the /proc/sys/net/ipv4/ping_group_range file



                      $ cat /proc/sys/net/ipv4/ping_group_range



                      It is "1 0" by default, meaning
                      that nobody (not even root) may create ping sockets. Setting it to "100
                      100" would grant permissions to the single group (to either make
                      /sbin/ping g+s and owned by this group or to grant permissions to the
                      "netadmins" group), "0 4294967295" would enable it for the world, "100
                      4294967295" would enable it for the users, but not daemons.



                      so any devices with other than "0 4294967295" cant access from an android java application



                      In emulator you can test this be resetting



                      sysctl -w net.ipv4.ping_group_range="0 0" // to some range






                      share|improve this answer













                      From the socket(2) man page ping access in the device is restricted by the content of the /proc/sys/net/ipv4/ping_group_range file



                      $ cat /proc/sys/net/ipv4/ping_group_range



                      It is "1 0" by default, meaning
                      that nobody (not even root) may create ping sockets. Setting it to "100
                      100" would grant permissions to the single group (to either make
                      /sbin/ping g+s and owned by this group or to grant permissions to the
                      "netadmins" group), "0 4294967295" would enable it for the world, "100
                      4294967295" would enable it for the users, but not daemons.



                      so any devices with other than "0 4294967295" cant access from an android java application



                      In emulator you can test this be resetting



                      sysctl -w net.ipv4.ping_group_range="0 0" // to some range







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 19 at 6:02









                      Nasif NoorudeenNasif Noorudeen

                      3219




                      3219













                      • ...and this answers the original question how?

                        – Stefan Becker
                        Jan 19 at 6:22



















                      • ...and this answers the original question how?

                        – Stefan Becker
                        Jan 19 at 6:22

















                      ...and this answers the original question how?

                      – Stefan Becker
                      Jan 19 at 6:22





                      ...and this answers the original question how?

                      – Stefan Becker
                      Jan 19 at 6:22


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7451200%2fhow-to-icmp-ping-on-android%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Liquibase includeAll doesn't find base path

                      How to use setInterval in EJS file?

                      Petrus Granier-Deferre