Best way to ping an ip address and see if it can be reached?

I’m trying to check connectivity from one machine to another. I tried Unity’s Ping but that is simply not useable as it just shows whether we got any response and the time it took. For example if my router gives a “destination net unreachable” Unity Ping interprets that too as a response and shows a response time even though we didn’t really get a response from the address we were pinging.

So I’m thinking about using the .net Ping classes but there I’ve run into a problem with running it asynchronously. I’m not really familiar with c# async functions. Can they be easily run inside Unity coroutines?

I tried this:

IEnumerator BetterPing() {
    System.Net.NetworkInformation.Ping pinger = new System.Net.NetworkInformation.Ping();
    Task<PingReply> pingTask = pinger.SendPingAsync(loader.config["serverAddress"]);
    yield return new WaitUntil(() => pingTask.IsCompleted);
    UnityEngine.Debug.Log(pingTask.Result.Status.ToString());
}

But that just hangs the entier app and doesn’t continue to the next frame until the ping task has completed. Is there a better way to do this? Or a better method to check connectivity to another machine (and I don’t mean to a server, internet or website etc, I mean simply from a pc to a pc)?

I suspect ping is the wrong tool for what you’re needing. Computers and routers are often configured to not respond to pings, home routers this is typically a default configuration. Devices that are behind a NAT router won’t be what responds to the ping, it will be the router itself if at all, so it doesn’t tell you if the device you’re trying to contact is still there.

This is for a bespoke application on a network of computers connected via a private mobile vpn. I have the routers and computers configured so that they do respond to pings if they are working correctly.