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): public class Net : MonoBehaviour { public Text console; // Start is called before the first frame update void Start() { float start = Time.realtimeSinceStartup; System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); ping.PingCompleted += PingCompleted; ping.SendAsync("8.8.8.9", null); float end = Time.realtimeSinceStartup; Debug.Log("Time: " + (end - start) + " seconds"); } void PingCompleted(object sender, System.Net.NetworkInformation.PingCompletedEventArgs e) { console.text = "Time: " + e.Reply.RoundtripTime.ToString(); } } Since 8.8.8.9 is an apparently unresponsive address, you can see the difference. Debug.Log() is telling me 4.68 seconds.
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?
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): byte[] ip = new byte[4]{127,0,0,1}; //<--your ip goes here obviously. (as 4 bytes) System.Net.IPAddress addr = new System.Net.IPAddress( ip ); ping.SendAsync(addr, ms_timeout, null); This of course means that you need to know the ip of server beforehand.
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.
I noticed for me UnityEngine.Ping stops working above about 700ms. @Icaro-DLima can you say something about how well System.Net.NetworkInformation.Ping works across platforms (at least iOS and android)? According to this so post there might be problems on android: https://stackoverflow.com/questions/54910454/ping-on-android-in-unity See also my SO post: https://stackoverflow.com/questions/62719258/unity-ping-only-works-up-to-700ms
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.