tags:

views:

58

answers:

1

Hi I am developing a Ping application for Android 2.2.

I try my code and works Ok but only in local Ips, thats 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 followiing line at Manifest XML too:

  </application>

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

</manifest> 

Any help ??? Thank You :)

+1  A: 

Maybe ICMP packets are blocked by your (mobile) provider. If this code doesn't work on the emulator try to sniff via wireshark or any other sniffer and have a look whats up on the wire when you fire the isReachable() method.

You may also find some info in your device log.

Luminger
I have tested in a Android 2.2 Tablet connected via Wifi i dont use any Momile Provider. Thanks.
Luks89
Then your next step should be to sniff the traffic (not) generated by your device. There are many things which could went wrong here...
Luminger
It's unlikely that it's cause by blocked ICMP, as the Android documentation says that `InetAddress.isReachable` first tries to ping via ICMP if that fails it tries to ping on TCP port 7 (Echo)
Tseng
According to http://android.git.kernel.org/?p=platform/libcore.git;a=blob;f=luni/src/main/java/java/net/InetAddress.java;hb=HEAD ICMP will not be used if you don't specify an interface. isReachable only uses the TCP Echo stuff, seems to be some strange behavior or a firewalling problem or something else... Are you sure you are able to access the internet from your application? Ever tried to recieve a file via HTTP?
Luminger
how can I test that my app can access to the internet??
Luks89