aspose 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
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: 3952
    
  17

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: 39548
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
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: 39548
    
  27
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: 39548
    
  27
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
 
Similar Threads
configure gmail,yahoomail in android application
why android ????
java on mobile
the screen doesn't have a keyboard when I am coding android in eclipse.
Can't Install Android platforms on Android SDK