Search Unity

Ping.SendAsync() Too Slow!

Discussion in 'Multiplayer' started by IcaroDLima, Oct 31, 2019.

  1. IcaroDLima

    IcaroDLima

    Joined:
    Aug 6, 2019
    Posts:
    25
    Hello, I have a use case where I need to know package losses too, so I'm using System.Net.NetworkInformation.Ping instead of UnityEngine.Ping. The problem is that apparently the Ping.SendAsync() function is taking too long.

    Here is my minimal verifiable example:
    Code (CSharp):
    1. public class Net : MonoBehaviour
    2. {
    3.     public Text console;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         float start = Time.realtimeSinceStartup;
    9.  
    10.         System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
    11.         ping.PingCompleted += PingCompleted;
    12.         ping.SendAsync("8.8.8.9", null);
    13.  
    14.         float end = Time.realtimeSinceStartup;
    15.  
    16.         Debug.Log("Time: " + (end - start) + " seconds");
    17.     }
    18.  
    19.     void PingCompleted(object sender, System.Net.NetworkInformation.PingCompletedEventArgs e)
    20.     {
    21.         console.text = "Time: " + e.Reply.RoundtripTime.ToString();
    22.     }
    23. }
    Since 8.8.8.9 is an apparently unresponsive address, you can see the difference. Debug.Log() is telling me 4.68 seconds.
     
    IgorAherne likes this.
  2. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    If the IP addr. you're pinging doesn't exist it will take some time before an error occurs. I believe there are options that you can pass to the Ping command to specify a timeout, so that it returns earlier if no response is obtained.

    Also, I am not sure how sending ICMP requests is related to measuring packet loss?
     
    Joe-Censored likes this.
  3. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    For me this was due to Dns.GetHostName() (function which is called during SendAsync). It took almost 5 seconds trying to resolve the ip from the url string I was passing. Using this instead, solved the lag:

    Code (CSharp):
    1.  
    2.         byte[] ip = new byte[4]{127,0,0,1}; //<--your ip goes here obviously. (as 4 bytes)
    3.         System.Net.IPAddress addr = new System.Net.IPAddress( ip );
    4.         ping.SendAsync(addr, ms_timeout, null);


    This of course means that you need to know the ip of server beforehand.
     
    Last edited: Apr 29, 2020
    p-oth-aw likes this.
  4. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    You should start measuring the ping time after the DNS query is completed, otherwise you're indeed timing two operations, not just the roundtrip of the request.
     
    IgorAherne likes this.
  5. IARI

    IARI

    Joined:
    May 8, 2014
    Posts:
    70
  6. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    Have you found a better solution? I'm thinking about using System.Net.Ping instead of Unity.Ping. I will have to try on iOS at some point to see if there's a difference unless anyone else knows.

    It's very silly you can't specify the timeout yourself.