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

Pings and using isDone still works on airplane mode?

Discussion in 'Scripting' started by Kristen182, Jul 11, 2019.

  1. Kristen182

    Kristen182

    Joined:
    Aug 10, 2018
    Posts:
    42
    I am using pings in my game to determine if the user has an internet connection. However, they're not working as expected. When I install my game on my phone and put my phone on airplane mode, the pings are still completing. Any ideas why this is happening? Am I using pings incorrectly? Here's a shortened version of the logic I'm using.

    Code (CSharp):
    1. public IEnumerator StartPing(string ip)
    2.     {
    3.  
    4.         Ping p = new Ping(ip);
    5.         float startTime = Time.time;
    6.  
    7.         while (!p.isDone && Time.time < startTime + 5.0f)
    8.         {
    9.             yield return new WaitForSeconds(0.1f);
    10.         }
    11.         if (p.isDone) //if the ping completed
    12.         {
    13.                 Debug.Log("PING: successful");      
    14.         }
    15.         else
    16.         {
    17.             Debug.Log("Ping: failed. IP:" + ip);
    18.         }
    19.     }
    20.  
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    It seems there's no option to get another ping result except time. So i suppose this done flag is set to true after host replies or times out. Check p.time value to determine that. May be there's somethin like -1 or NaN?
     
  3. Kristen182

    Kristen182

    Joined:
    Aug 10, 2018
    Posts:
    42
    p.time gave me -1! If I try it with airplane mode off, I get 18 (not sure what unit p.time is in, calculating response time using Time.time - startTime gives me 0.1005859).

    Interesting how using p.time is the only reliable way to check if a ping actually went through. Thank you, I never would have thought of that!
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    I believe this 18 is in milliseconds.
     
  5. Kristen182

    Kristen182

    Joined:
    Aug 10, 2018
    Posts:
    42
    That makes sense, thanks again!