locked
How to get the DNS servers IP address RRS feed

  • Question

  • User1278 posted

    I am doing some custom DNS lookups to handle SRV records. To make this work, I need the IP address of the current DNS server from the device. This code works in the simulator, but on the device, no DNS servers are found. Any ideas?

        public static IPEndPoint[] GetDnsAdress()
        {
    
    
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            List<IPEndPoint> ipAddresses = new List<IPEndPoint>();
    
            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
                IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
    
                foreach (IPAddress dnsAdress in dnsAddresses)
                {                       
                    IPEndPoint currentEndpoint = new IPEndPoint (dnsAdress, 53);
                    if(!ipAddresses.Contains(currentEndpoint))
                    {
                        ipAddresses.Add (currentEndpoint);
                    }
                }
            }
    
            return ipAddresses.ToArray ();
        }
    
    Thursday, July 25, 2013 11:55 PM

All replies

  • User105518 posted

    Was surprised to see this post from 2013 and still not answered. I am facing same issue for Android.

    Raymond, were you able to find solution? will be really appreciated.

    Wednesday, May 20, 2015 8:50 AM
  • User1278 posted

    Sorry, no solution. I am working in iOS and apparently you just cant get the DNS servers address, not allowed.

    Thursday, June 25, 2015 3:07 PM
  • User105518 posted

    Thanks Raymond for your reply. I am able to get it while on WiFi now, but don't know how to get in on cellular data (3G/4G). Thanks and Regards.

    Friday, June 26, 2015 3:31 AM
  • User1278 posted

    Can you share how you accomplish this on Wi-Fi ? Thanks.

    Friday, June 26, 2015 8:26 PM
  • User105518 posted

    I used solution from this answer on stackoverflow. Thanks.

    Monday, June 29, 2015 1:53 AM
  • User1278 posted

    This is only for Android. And pretty sure will not work in either case for Xamarin Forms.

    Monday, June 29, 2015 11:57 AM
  • User27434 posted

    If it's of use to anyone else I found stackoverflow. From there I created a binding to xcodeproject and now the Xamarin.iOS can get the DNS server address. Seems to work well for WiFi and 3G.

    Tuesday, June 28, 2016 6:25 AM
  • User1278 posted

    @TomKieronski thanks for you link. Any change you have the binding that you can share? Thanks!

    Thursday, January 4, 2018 4:22 PM
  • User1278 posted

    Or share the apideffinitions and structsandenums files. Thanks again.

    Thursday, January 4, 2018 6:02 PM