How do I find the DNS servers in Android from a Java program?

前端 未结 4 1199
梦谈多话
梦谈多话 2021-02-19 00:41

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

4条回答
  •  无人共我
    2021-02-19 01:00

    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.

    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.


    Edit - android.net

    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.

提交回复
热议问题