Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Dns.GetHostEntry failing on Mac but not PC?

Discussion in 'Scripting' started by psychicparrot, Mar 6, 2008.

  1. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Anyone tried using Dns.GetHostEntry in C#? Had any success?

    On Windows, this call is successful and everything gets resolved. On Mac, it never seems to come back out of the call.

    I read online that there was a bug in Mono but it got fixed (allegedly) in a newer version ... do we know what implementation of Mono that Unity is using?

    Thanks!
    Jeff.
     
  2. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    I'm using the following code to test internet and it works fine on both platforms.

    Code (csharp):
    1. public static bool IsInternetAvailable()
    2.     {
    3.         try
    4.         {
    5.             IPHostEntry thisIPHostEntry = Dns.GetHostEntry("www.google.com");
    6.             return true;
    7.         }
    8.         catch
    9.         {
    10.             return false; // host not reachable.
    11.         }
    12.     }
     
  3. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
    Ah ... seems you can't do this with an IP address. Maybe I'm missing something, but if I try Google's IP address like this:


    Code (csharp):
    1.  
    2. public static bool IsInternetAvailable()
    3.     {
    4.         try
    5.         {
    6.             IPHostEntry thisIPHostEntry = Dns.GetHostEntry("72.14.205.103");
    7.             return true;
    8.         }
    9.         catch
    10.         {
    11.             return false; // host not reachable.
    12.         }
    13.     }
    14.  
    15.  
    This will fail both in the IDE and the browser.
     
  4. psychicparrot

    psychicparrot

    Joined:
    Dec 10, 2007
    Posts:
    884
  5. marblez

    marblez

    Joined:
    Oct 6, 2008
    Posts:
    14
    But does this work for you in a webplayer? I'm getting a SocketException from this Dns.GetHostEntry call in the webplayer but it works fine in standalone and in the editor. The exception is "No such host is known". I tried my "my.address.com", "http://my.address.com", "http://my.address.com/", and the literal IP address.
     
  6. azupko

    azupko

    Joined:
    Nov 4, 2008
    Posts:
    21
    just to bump this alive -

    I'm working with gethostentry and on mac, like discussed here, it works the FIRST time in the webplayer - subsequent calls fail (after a browser refresh or leaving and coming back later). Restarting the browser entirely corrects the problem. I am currently hacking around this by caching the correctly returned results in playerprefs, and using those if the entry is incorrect. Does anyone have any more up-to-date information on this problem, or if there is something worth trying? When it fails, it is not an explicit exception - rather, only 1 ipv6 is returned that is not capable of being bound.

    thanks ahead of time for any thoughts!
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you don't need to create a host entry with ips ... the only reason to use this class basically is to get the ip because the network commands don't accept urls, just ips. (I for example use it to get the ip of master server, facilitator and connection tester from url)
     
  8. eric@mixamo

    eric@mixamo

    Joined:
    Nov 10, 2009
    Posts:
    3
    Thanks for the idea of using PlayerPrefs as a workaround DNS cache.
     
  9. MichaelDuncan

    MichaelDuncan

    Joined:
    Oct 12, 2009
    Posts:
    12
    Bump!

    This is still an issue. We came to the same store in the player prefs "solution". But shame on unity for ignoring such an annoying issue.
     
  10. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Unity cannot do anything.

    This is an issue with Mono, and so an external utility of Unity.

    3.0 may remedy the situation depending on how Mono fixed it's bugs, but there is nothing Unity can reasonably do about it.
     
  11. ritchparker

    ritchparker

    Joined:
    Apr 9, 2008
    Posts:
    26
    Has anyone tried Dns.GetHostByName("www.google.com")? This seems to work fine for me on the Mac (running 10.5.8). I did a web player build and ran that, seems to work multiple times in safari (running from the local html file).
     
  12. ghellee

    ghellee

    Joined:
    Dec 6, 2017
    Posts:
    5
    Awesome!!! I tried a lot of ways and this worked the best! The performance is fantastic! It's awaitable!

    Have tested it on Editor and Android. Will try iOS and Browser soon.