Dns.GetHostEntry() in MonoTouch

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Dns.GetHostEntry() in MonoTouch

pseabury
This may be a Mono issue, but I'll start here first.  As I understand it, if I call Dns.getHostEntry(string.Empty), I should get a list of all IPv4 addresses of the local host (http://msdn.microsoft.com/en-us/library/ms143998(v=vs.80).aspx).

What I actually get is a SocketException (Code=11001, HostNotFound, "No such host is known").  I get this whether connected or not, wifi, wwan, airplane mode.

Am I doing something wrong here?  I see a similar discussion here (http://monotouch.2284126.n4.nabble.com/Checking-local-network-availability-tp4655233p4655264.html), but he didn't seem to have any problems.

I'm doing this because the Reachability code on MT doesn't work reliably for me, and this seems like a straightforward substitution for it.
Reply | Threaded
Open this post in threaded view
|

Re: Dns.GetHostEntry() in MonoTouch

dermotos
is this on the simulator or the device? try putting .local at the end of the IP Address, or just ".local"
Reply | Threaded
Open this post in threaded view
|

Re: Dns.GetHostEntry() in MonoTouch

pseabury
It's on the Device.  If I use Dns.GetHostAdresses(hostName) with String.Empty, or "localhost", or Dns.GetHostName(), or Dns.GetHostName() + ".local", or ".local", I only ever get an actual IPAddress other than loopback when connected via WiFi.  If I'm connected via WWAN (cellular), all I ever get is Loopback. 

Is this as designed?

Paul


On Sat, Jul 28, 2012 at 6:41 PM, dermotos [via MonoTouch] <[hidden email]> wrote:
is this on the simulator or the device? try putting .local at the end of the IP Address, or just ".local"


If you reply to this email, your message will be added to the discussion below:
http://monotouch.2284126.n4.nabble.com/Dns-GetHostEntry-in-MonoTouch-tp4656219p4656220.html
To unsubscribe from Dns.GetHostEntry() in MonoTouch, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Dns.GetHostEntry() in MonoTouch

dermotos
This is the code i've been using for months with zero issues. It returns the first IP4 address, which always seems to be the correct address.

   public static IPAddress GetIPAddress()
        {

            IPAddress[] availableAddresses = Dns.GetHostAddresses(Dns.GetHostName() + ".local");
            IPAddress activeAddress = null;

            foreach (IPAddress ip in availableAddresses)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    activeAddress = ip;
                    break;
                }
            }

            return activeAddress;
        }

        public static IPAddress GetIPAddress(string hostName)
        {
            IPAddress[] availableAddresses = Dns.GetHostAddresses(hostName);
            IPAddress activeAddress = null;

            foreach (IPAddress ip in availableAddresses)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    activeAddress = ip;
                    break;
                }
            }

            return activeAddress;
        }