2

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.

Curtis
  • 5,794
  • 8
  • 50
  • 77

1 Answers1

5

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 ...
            }
        }
    }  
}
Jason
  • 86,222
  • 15
  • 131
  • 146
  • Xamarin has a bug in their implementation of NetworkInterface.GetAllNetworkInterfaces(). The bug is that the interfaces that are returned in the list do not have their 'OperationalStatus' set. They all are OperationalStatus.Unknown. I guess it is ok to just 'assume' that the returned interfaces are 'Up' even though they say 'Unknown'? – Curtis Jul 26 '13 at 17:51
  • Network.GetAllNetworkInterfaces() throws a host exception for me. I've asked about this in http://stackoverflow.com/questions/25593642/networkinterface-getallnetworkinterfaces-call-throws-socketexception-on-iphone and would appreciate any info you might have. – bentayloruk Aug 31 '14 at 16:09
  • While the answer seam to work fine on all 3 (iOS, Android, and UWP), on UWP it returns 169.254... address which is not a valid but MS reserved address that does not even show in ipconfig interfaces – pixel Jun 20 '19 at 15:24
  • 1
    I was using system.net.dns in my xamarin forms app and all of a sudden, iOS started throwing an exception that it could not DNS name resolve my iOS device. This fixed that problem. – George M Ceaser Jr Mar 13 '22 at 17:00
  • This solution (NetworkInterface.GetAllNetworkInterfaces) is no longer working on Android. On my OnePlus 7T running Android 11 - netInterface.NetworkInterfaceType is always set to 0. This used to work but recently stopped. It appears I have to use Dns.GetHostAddresses(Dns.GetHostName()); for Android and the above solution for iOS - Kind of a pain. I open a ticket in GitHub about the error https://github.com/dotnet/runtime/issues/76493 – George M Ceaser Jr Oct 02 '22 at 20:45