如何找到从Java程序在Android上的DNS服务器? [英] How do I find the DNS servers in Android from a Java program?

查看:705
本文介绍了如何找到从Java程序在Android上的DNS服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.net.InetAddress.GetByName(String host)在方法只能返回 A 记录,以便查找其他记录类型我需要能够使用 dnsjava 库发送DNS查询。

The java.net.InetAddress.GetByName(String host) method can only return A records so to lookup other record types I need to be able to send DNS queries using the dnsjava library.

不过通常依赖于能够解析的/etc/resolv.conf 或类似的查找DNS服务器地址,并没有在Android上正常工作。

However that normally relies on being able to parse /etc/resolv.conf or similar to find the DNS server addresses and that doesn't work on Android.

在Android目前的DNS设置,可以很明显只能从外壳中使用 getprop 命令获得。

The current DNS settings on Android can apparently only be obtained from within a shell by using the getprop command.

谁能告诉我如何从Java之外的这些设置不是产卵壳的Runtime.exec()和解析输出 getprop

Can anyone tell me how to get those settings from Java other than by spawning a shell with Runtime.exec() and parsing the output from getprop?

推荐答案

DNS协议是不那么复杂 - 你不能只是做了DNS访问使用原始套接字(TCP或UDP)?经过快速看一下dnsjava多科似乎提供低级别的DNS支持,以协助这一点。

The DNS protocol is not that complex - can't you just do the DNS accesses using raw sockets (either TCP or UDP)? After a quick look at the dnsjava doco it seems to provide low level DNS support to assist with this.

其他可能的方向是,从dnsjava,以消除/etc/resolv.conf中的依赖。我会考虑使用getprop在你的启动脚本在JVM中设置属性,或创建你的应用程序的目录中的本地化resolv.conf文件,从中可以读出所需要的信息。换言之,使用getprop而不是尝试把它拉在一旦在JVM将要信息到JVM注入。当然,创建dnsjava可以直接使用应该是可行的文件。

The other possible direction is, starting with dnsjava, to remove the dependence on /etc/resolv.conf. I would think about using getprop in your launch script to set properties in the JVM, or to create a localized resolv.conf file in your app's directory from which you can read the information needed. In other words, use getprop to inject information into the JVM instead of attempting to pull it in once the JVM is going. Surely creating a file that dnsjava can use directly should be doable.


看起来android.net.ConnectivityManager将提供您的NetworkInfo的使用getAllNetworkInfo数组()。然后使用android.net.NetworkUtils.runDhcp()来得到一个DhcpInfo对于任何给定的网络接口 - 该DhcpInfo结构具有DNS1和DNS2该接口的IP地址。意外的是,DNS的是整型,因此暗示IP4只,虽然。

It looks like android.net.ConnectivityManager will deliver you an array of NetworkInfo's using getAllNetworkInfo(). Then use android.net.NetworkUtils.runDhcp() to get a DhcpInfo for any given network interface - the DhcpInfo structure has the IP address for dns1 and dns2 for that interface. Surprised that the DNS's are int, therefore implying IP4 only, though.

这篇关于如何找到从Java程序在Android上的DNS服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆