File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes android DNS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "android DNS" Watch "android DNS" New topic
Author

android DNS

qiang Migic
Greenhorn

Joined: Feb 27, 2013
Posts: 8
how to realize ping, tracert and DNS in Android?
Please help me .thank you!!
or you can tell me what technology you use it?
jni + ndk or Runtime.getRuntime (). exec (cmdString);
I will be grateful!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

A ping is pretty easy to replicate using InetAddress (<- link). I am not sure what you expect the DNS command to do, so if you explain what you want, perhaps we can help. I don't know how to do the traceroute, but I would search for the Java equivalent and check here to see if those classes are available on the Android platform.


Steve
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
JNI/NDK doesn't provide access to any networking functionality, so that's not an option.

Using Runtime.exec you use Android's built-in ping command. You can emulate traceroute by doing successive pings with an ever higher TTL (starting at 1, and incrementing it until you reach the destination host).

For DNS, check out the DNSJava library; it works fine on Android.
qiang Migic
Greenhorn

Joined: Feb 27, 2013
Posts: 8
in java I call the code below:
public static void main(String[] args) {

trancert("www.yahoo.com");
}
public static String trancert(String ip){
StringBuffer sb =new StringBuffer();
String cmdString ="tracert -h 10 ";
java.lang.Process progress = null;
BufferedReader br = null;
String s= "";
try {
progress = Runtime.getRuntime().exec(cmdString +ip);
InputStream in = progress.getInputStream();

br = new BufferedReader(new InputStreamReader(in));
while( (s =br.readLine() )!= null){
sb.append(s);
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
This code can be normal operation.

but in android I use String cmdString ="tracert -h 10 "; or String cmdString ="traceroute -h 10 "; it doesn't work.
I capture the Exception is "working directory:null Environment: null".
I'd like to run on all mobile phones Including no root of mobile phone.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
Android has no usable traceroute built in, that's why I mentioned the workaround using ping.

If you haven't yet done so, install a terminal emulator (like the one from Jack Palevich in the Play Store) so you can easily find out which commands Android does or does not support.
qiang Migic
Greenhorn

Joined: Feb 27, 2013
Posts: 8
OK ,you are right ,I understand what you said.
But ,There is a problem you may not know.
After my test, android could support the ping command.
But this command in android has some problems
for example:
1 . String strCmd ="ping -c 1 -w 100 ";
if the IP is wrong,( 192.168.6.1) it will be long time to return the results.That is very bad experience.
2 I changed to String cmdString ="ping -c 1 -w 2 -i 1 ";
IP test normal 。but if the DNS(www.yahoo.com) is inexistence (like ljfjdlas.fjdsla) android Still can ping pass.

so you told me use the (ping and TTL++ )to realize the problem.We must first solve the function of ping.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
There is an option where you can set the timeout, maybe to 2 seconds or so.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: android DNS