Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Ping External IP from Java Android

Tags:

java

android

I am developing a Ping application for Android 2.2.

I try my code and it works, but only in local IPs, that's my problem I want to do ping to external servers too.

Here is my code:

  private OnClickListener milistener = new OnClickListener() {     public void onClick(View v) {         TextView info = (TextView) findViewById(R.id.info);         EditText edit = (EditText) findViewById(R.id.edit);         Editable host = edit.getText();         InetAddress in;         in = null;         // Definimos la ip de la cual haremos el ping         try {             in = InetAddress.getByName(host.toString());         } catch (UnknownHostException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         // Definimos un tiempo en el cual ha de responder         try {             if (in.isReachable(5000)) {                 info.setText("Responde OK");             } else {                 info.setText("No responde: Time out");             }         } catch (IOException e) {             // TODO Auto-generated catch block             info.setText(e.toString());         }     } }; 

Ping 127.0.0.1 -> OK
Ping 8.8.8.8 (Google DNS) -> Time Out

I put the following line at Manifest XML too:

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

Can anyone suggest me where I'm doing wrong?

like image 659
Luks89 Avatar asked Oct 11 '10 10:10

Luks89


People also ask

Can I run ping from Android phone?

Using Ping App Grab your smartphone, and launch the Ping app. If you haven't installed the app yet please go to Play Store and download it first. Then tap the start button. Wait for a second and you will see the results of the data connection on your Android smartphone screen.

Can you ping in Java?

ICMP differs from transport protocols such as TCP and UDP in that it is not typically used to exchange data between systems. This Java Program pings an IP address in Java using InetAddress class. It is successful in case of Local Host but for other hosts this program shows that the host is unreachable.


1 Answers

I tried following code, which works for me.

private boolean executeCommand(){         System.out.println("executeCommand");         Runtime runtime = Runtime.getRuntime();         try         {             Process  mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");             int mExitValue = mIpAddrProcess.waitFor();             System.out.println(" mExitValue "+mExitValue);             if(mExitValue==0){                 return true;             }else{                 return false;             }         }         catch (InterruptedException ignore)         {             ignore.printStackTrace();             System.out.println(" Exception:"+ignore);         }          catch (IOException e)          {             e.printStackTrace();             System.out.println(" Exception:"+e);         }         return false;     } 
like image 156
krishna5688 Avatar answered Sep 19 '22 17:09

krishna5688