Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios monotouch中的默认DNS服务器_Ios_Xamarin.ios_Dns - Fatal编程技术网

Ios monotouch中的默认DNS服务器

Ios monotouch中的默认DNS服务器,ios,xamarin.ios,dns,Ios,Xamarin.ios,Dns,我想知道如何在monotouch中获得默认DNS服务器 此代码在模拟器中运行良好,但在设备上提供0条记录 NetworkInterface.GetAllNetworkInterfaces(); foreach (IPAddress ipAddr in ipProps.DnsAddresses) Console.WriteLine(ipAddr); 另一方面,此代码在模拟器和设备上都有效: IPHostEntry he = Dns.GetHostEntry(domain); dns = h

我想知道如何在monotouch中获得默认DNS服务器

此代码在模拟器中运行良好,但在设备上提供0条记录

NetworkInterface.GetAllNetworkInterfaces();
foreach (IPAddress ipAddr in ipProps.DnsAddresses)
   Console.WriteLine(ipAddr);
另一方面,此代码在模拟器和设备上都有效:

IPHostEntry he = Dns.GetHostEntry(domain);
dns = he.HostName.ToString();

有了这些,我假设DNS服务器地址存储在某个地方。我的意思是,它是可接近的。如何获得IP?

我不相信iOS上存在这样的API(但我很高兴被证明是错误的)。其他需要这些信息的项目依赖于(比如使用众所周知的DNS服务器静态地址)来克服这一问题

现在,原因代码如下所示:

        var all = NetworkInterface.GetAllNetworkInterfaces ();
        foreach (NetworkInterface ni in all) {
            var props = ni.GetIPProperties ();
            foreach (var dns in props.DnsAddresses) {
                Console.WriteLine (dns);
            }
        }
在模拟器上工作是因为它是模拟器而不是模拟器。IOW主机(Mac)允许的事情比真正的iOS设备要多得多

更准确地说,
props
将是
System.Net.NetworkInformation.MacOsIPInterfaceProperties
的一个实例,它继承自
UnixIPInterfaceProperties
,以及
/etc/resolv.conf
文件(iOS禁止您的应用程序读取)


第二种情况是调用
Dns.GetHostEntry
,它会进入下一步,但最终调用
gethostname
,这不需要调用方知道Dns服务器地址。

这将在MonoTouch中获取IP地址:-

    public string GetIPAddress()
    {
        string address = "Not Connected";
        try
        {
            #if SIM
                address = IPAddress.FileStyleUriParser("127.0.0.1"); 
            #else
                string str = Dns.GetHostName() + ".local";
                IPHostEntry hostEntry = Dns.GetHostEntry(str);
                address = (
                           from addr in hostEntry.AddressList
                           where addr.AddressFamily == AddressFamily.InterNetwork
                           select addr.ToString()
                           ).FirstOrDefault();
            #endif
        }
        catch (Exception ex)
        {
            // Add error handling....
        }
        return address;
    }

请注意使用模拟器和设备之间的区别。

第一个代码段不会编译。我假设您使用的是类似于?的东西,根据SDK,此信息不可用。[1] :我没有尝试访问resolv.conf。我需要获取当前的DNS服务器IP。我很确定我在C/Obj C中看到过这样的例子,他们正试图做和你一样的事情,而且缺乏其他建议,这让我相信SDK不提供访问这些信息的权限。你读过吗?不知道,也许有帮助: