Search Unity

Fast and reliable way to check if a device is connected to any network

Discussion in 'Multiplayer' started by GabrielS, Sep 5, 2013.

  1. GabrielS

    GabrielS

    Joined:
    Sep 5, 2013
    Posts:
    6
    Which is the most straightforward way to check if the device has a valid network connection (not necessarily an Internet one)? I.e. something equivalent to .Net's System.Net.NetworkInterface.GetIsNetworkAvailable() (which is not implemented in Unity/Mono). I've seen the Application.internetReachability method in the documentation, but i'm not sure if this is the way to go.

    Also, it would be highly useful if there were a technique to let the framework announce when the said connectivity status has changed, much like .Net's NetworkInformation.NetworkStatusChanged event. Is there anything equivalent in Unity?
     
  2. eskimojoe

    eskimojoe

    Joined:
    Jun 4, 2012
    Posts:
    1,440
    Can you try ping the IP address or do a DNS look-up.


    If you get 127.0.0.1 for the website, the person is probably off-line
     
  3. GabrielS

    GabrielS

    Joined:
    Sep 5, 2013
    Posts:
    6
    Thanks for the suggestion, but pinging or DNS-ing the server each time before i want to make a request to it (which may happen quite often) is time-consuming and a bit redundant - i might as well send the actual request and see that it fails. What i want to do, actually, is to "short-circuit" very quickly the entire scheme if i can determine that there's no way to reach the server (i.e. there is no network adapter connected to a local network, etc.) without sending an actual packet. On .Net you could do this simply by querying the state of the hardware (NetworkInterface.GetIsNetworkAvailable( ) would do that for you) - if no adapter is connected to any network, there's definitely no way your request will reach the server. So, on this "preconditional" step i'm not really interested to see if i can actually reach the server, only to see if the basic condition of network availability is met - this, of course, is not synonymous with full Internet connection, but if it's not satisfied I can quickly and neatly prevent and queue/delay the web request.
     
  4. eskimojoe

    eskimojoe

    Joined:
    Jun 4, 2012
    Posts:
    1,440
    You check it once on startup.

    The recommended practice is to do a DNS resolve on the URL to an IP address. During that time, this is where you do the check.

    After getting the IP address, start the multi-player game via Unity networking passing an IP address to connect.


    If you store the IP address of your game-server and later shut-down or move ISPs, your customers will get really messed-up as nobody can play multi-player.
     
  5. GabrielS

    GabrielS

    Joined:
    Sep 5, 2013
    Posts:
    6
    It has nothing to do with this scenario. I don't use the IP address of the server - which is not a multiplayer server anyway -, only its URL. I just want to prevent and postpone any requests to that server if i'm sure there's no network connectivity available.
     
  6. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    The presence of a network interface is a poor indication of connectivity - if you are planning to eventually contact a server, the most reliable thing to do really is to just try to talk to it, and in practice it's usually a very quick test to perform. Firing a hand-made DNS packet at a root server is also fairly trivial.

    If on the other hand you want to be able to do networking even without Internet access (e.g. contacting other devices on a local network) then you do indeed need a way to enumerate local interfaces.

    It may depend what device you run on, but certainly in the editor Network.player.ipAddress seems to work - for me it's filled in with my LAN IP address. Also note that on mobile you have Application.internetReachability that informs you whether the device has a mobile connection, a wifi connection, or no connection. The docs make it sound like it just checks the presence of the interface, not whether it can actually talk to the Internet through the interface.
     
  7. GabrielS

    GabrielS

    Joined:
    Sep 5, 2013
    Posts:
    6
    Yes, the server might run o a local network as well. So after all Application.internetReachability seems to be what I need.

    Thanks!