[英]No GatewayAddresses when looping through NetworkInterfaces on Xamarin.Android
I'm trying to scan the local IP address of a mobile device to ping all other devices in a network and discover local IP addresses.我正在尝试扫描移动设备的本地 IP 地址以 ping 网络中的所有其他设备并发现本地 IP 地址。 In the first step I'm retrieving the GatewayAddress of the local NetworkInterface like this:在第一步中,我像这样检索本地 NetworkInterface 的 GatewayAddress:
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
if (f.OperationalStatus == OperationalStatus.Up)
{
foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses)
{
ip = d.Address.ToString();
}
}
}
This works perfectly fine on iOS, however on Android the GatewayAdresses
have a count of 0
.这在 iOS 上工作得很好,但是在 Android 上, GatewayAdresses
的计数为0
。 As far as my research goes there has been bugs with NetworkInterface
s in Xamarin but they seem to be all fixed by now.就我的研究而言,Xamarin 中的NetworkInterface
存在错误,但现在它们似乎都已修复。
You could use the code to get the ipv6 and ipv4.您可以使用代码来获取 ipv6 和 ipv4。
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
if (f.OperationalStatus == OperationalStatus.Up)
{
//foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses)
//{
// var ip = d.Address.ToString();
//}
IPAddress ipv6Address = f.GetIPProperties().UnicastAddresses[0].Address; //This will give ipv6 address of certain adapter
IPAddress ipv4Address = f.GetIPProperties().UnicastAddresses[1].Address; //This will give ipv4 address of certain adapter
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.