我正在尝试枚举所有使用 iOS 的网络接口(interface)的 IP 地址和子网掩码。
System.Net.NetworkInformation.NetworkInterface[] nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (System.Net.NetworkInformation.NetworkInterface nic in nics)
{
var ipProps = nic.GetIPProperties();
// We're only interested in IPv4 addresses for this example.
var ipv4Addrs = ipProps.UnicastAddresses
.Where(addr => addr.Address.AddressFamily == AddressFamily.InterNetwork);
foreach (var addr in ipv4Addrs)
{
UnicastIPAddressInformation addrInfo = (UnicastIPAddressInformation)addr;
IPAddress ipAddr = addrInfo.Address;
IPAddress mask = addrInfo.IPv4Mask;
string x = mask.ToString(); // NotImplementedException is thrown here.
}
}
当我尝试获取 IP4 掩码时,它似乎抛出 NotImplementedException。似乎 Mono 没有实现这个属性。
是否有任何其他方法可以使用 Xamarin.iOS 类或 iOS API 调用来执行此操作?
最佳答案
我试图从头开始,所以我查看了 mono 基类库并重新使用了在那里找到的平台调用。我创建了一个小型 iPhone 应用程序,用于枚举网络接口(interface)并显示地址和掩码。
这是带有源代码的 github 存储库 - NetworkTest
关于c# - 如何使用 Xamarin.iOS 枚举 ipv4 地址和 ipv4mask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21524902/