1

因此,我正在使用 Xamarin 和 .net 在 iOS 项目中实现 uPNP。我一直在努力为本地设备(即运行程序的设备)获取有效的本地 IP 地址。

我尝试使用NetworkInterface.GetAllNetworkInterfaces(),但 Xamarin 对该方法的实现中有一个错误,它不起作用。

所以,我环顾四周,找到了一个简单的方法来完成这个。我尝试了以下方法:

IPAddress[] hostAddresses = Dns.GetHostAddresses(Dns.GetHostName());

以上引发了“无法解析主机...”异常(其中...是我的设备名称)。

所以它确实得到了我的设备名称,但它无法解析它。

此代码在 WPF 应用程序的 windows 下工作得很好。在带有 iPhone 或 iPad 模拟器的 MAC 上使用 Xamarin Studio 可以正常工作。

但是,当我尝试让 MAC 在我的实际 iPad 设备上启动应用程序时,出现以下异常:

System.Net.Sockets.SocketException:无法解析 /Developer/MonoTouch/Source/mono/mcs/class/System/System 中 System.Net.Dns.Error_11001 (System.String hostName) [0x00000] 的主机“Charrison”。 Net/Dns.cs:298 在 System.Net.Dns.hostent_to_IPHostEntry (System.String originalHostName, System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00082] 在 /Developer/MonoTouch/Source /mono/mcs/class/System/System.Net/Dns.cs:326 在 /Developer/MonoTouch/Source/mono/mcs/class/System 中的 System.Net.Dns.GetHostByName (System.String hostName) [0x0002a] /System.Net/Dns.cs:467 在 System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00061] 在 /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/Dns.cs :406 在 System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00065] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/Dns.cs:432 at UpnpUI_iOS.DeviceViewController.startButton_TouchUpInside (System.Object sender, System.EventArgs e) [0x0008c ] 在 /Users/engineering/Projects/UpnpUI_iOS/DeviceViewController.cs:83 在 MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] 在 /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:30 在(包装器托管到本机)MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 在 MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] 在 /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38DeviceViewController.startButton_TouchUpInside (System.Object sender, System.EventArgs e) [0x0008c] in /Users/engineering/Projects/UpnpUI_iOS/DeviceViewController.cs:83 at MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in /Developer/MonoTouch /Source/monotouch/src/UIKit/UIControl.cs:30 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] 在 /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38DeviceViewController.startButton_TouchUpInside (System.Object sender, System.EventArgs e) [0x0008c] in /Users/engineering/Projects/UpnpUI_iOS/DeviceViewController.cs:83 at MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in /Developer/MonoTouch /Source/monotouch/src/UIKit/UIControl.cs:30 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] 在 /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38在/Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:30 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr) 中激活 () [0x00000] ,intptr) 在 MonoTouch.UIKit.UIApplication.Main (System.String [] args, System.String principalClassName, System.String delegateClassName) [0x0004c] 在 /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs: 38在/Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:30 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr) 中激活 () [0x00000] ,intptr) 在 MonoTouch.UIKit.UIApplication.Main (System.String [] args, System.String principalClassName, System.String delegateClassName) [0x0004c] 在 /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs: 38
在 /Users/engineering/Projects/UpnpUI_iOS/Main.cs:17 中的 UpnpUI_iOS.Application.Main (System.String[] args) [0x00008]

如果有人知道一些不错且快速的方法来获取本地设备的有效 IP 地址,该地址将在我的 iPad 上使用 .net 和 Xamarin 实际运行,请告诉我。我真的很感激。

提前感谢您的有用建议。

4

1 回答 1

3

在论坛上从 Mike Bluestein 那里偷来的

foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces()) {
    if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
        netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet) {
        foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses) {
            if (addrInfo.Address.AddressFamily == AddressFamily.InterNetwork) {
                var ipAddress = addrInfo.Address;

                // use ipAddress as needed ...
            }
        }
    }  
}
于 2013-07-26T16:23:42.873 回答