ios - Monotouch 中的默认 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 = he.HostName.ToString();

有了这一切,我假设 DNS 服务器地址存储在某处。我的意思是它是可以访问的。如何获取其IP?

最佳答案

这将在 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;
    }

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

关于ios - Monotouch 中的默认 DNS 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9281335/

相关文章:

iphone - UITableViewCell 的 DetailTextLabel 为 null,缺少什么?

c# - Mono 的 DNS 刷新超时

ios - Xcode 6 - Save for iOS App Store Deployment 需要访问开发团队吗?

ios - 删除 CorePlot 图上的第一条和最后一条网格线

ios - 使用 Xamarin.Forms 更新 ListView 文本绑定(bind)会导致 iOS 上闪烁

c# - 如何在 Xamarin iOS SQLite 中添加 SQLiteOpenFlags.FullMutex 标志

ios - 获取对等依赖错误 react / react native

ios - ionic3 - 无效的 App Store 图标。 'YourApp.app' Assets 目录中的 App Store Icon 不能透明,也不能包含 alpha channel

wordpress - HTTP header 在非 www URL 上返回 404?

linux - 从 JAVA 刷新 linux DNS