Question

So, I'm implementing uPNP in an iOS project using Xamarin and .net. I have been struggling with getting a valid local ip address for the local device (ie. the device on which the program is running).

I've attempted to use NetworkInterface.GetAllNetworkInterfaces(), but there is a bug in Xamarin's implementation of that method and it doesn't work.

So, I looked around and found an easy way to accomplish this. I tried the following:

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

The above throws a 'Could not resolve host...' exception (where ... is my device name).

So it does get my device name, but then it cannot resolve it.

This code works just fine under windows in a WPF application. It works just fine using Xamarin Studio on a MAC with the iPhone or iPad simulator.

However, when I try to have the MAC launch the app onto my actual iPad device I get the following exception:

System.Net.Sockets.SocketException: Could not resolve host 'Charrison' at System.Net.Dns.Error_11001 (System.String hostName) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/Dns.cs:298 at System.Net.Dns.hostent_to_IPHostEntry (System.String originalHostName, System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00082] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/Dns.cs:326 at System.Net.Dns.GetHostByName (System.String hostName) [0x0002a] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/Dns.cs:467 at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00061] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/Dns.cs:406 at 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] 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 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] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at UpnpUI_iOS.Application.Main (System.String[] args) [0x00008] in /Users/engineering/Projects/UpnpUI_iOS/Main.cs:17

If anybody knows some nice and speedy way to get a valid IP address for the local device that will actually work on my iPad using .net with Xamarin, please let me know. I would really appreciate it.

Thanks in advance for your useful suggestions.

Was it helpful?

Solution

Stolen from Mike Bluestein in the forums

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 ...
            }
        }
    }  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top