Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

android DNS

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43076
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an option where you can set the timeout, maybe to 2 seconds or so.
 
Evil is afoot. But this tiny ad is just an ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic