A slightly improved version of the Ping class from .NET Core.
The standard .NET Ping class has a flaw. It only delivers a valid PingReply.RoundtripTime value for replies with status == IPStatus.Success. This is primarily a problem when using the Ping class to perform a traceroute, as most of the replies will have status IPStatus.TtlExpired. In this case, the Ping class has a perfectly valid round-trip-time already measured, but intentionally throws it out and constructs the PingReply with a RoundtripTime of 0.
Microsoft does not care to change this.
Many developers work around this issue by measuring the response time themselves using the Stopwatch class. Unfortunately, this delivers a significantly less-accurate measure of response time, as you are also measuring the time required for your Thread to resume after receiving the ping reply.
This project is a nearly-identical copy of the Ping class, and supporting classes, from .NET Core version 2.0.5. This version was chosen because it pre-dates several changes to the framework which made extracting the Ping class much more difficult.
These are the notable modifications:
- Namespaces have been changed for reasons of compatibility. The improved
Pingclass is located in theSmartPingnamespace. - All ping replies contain the
RoundtripTimeproperty as measured by the nativePingimplementation, even if the reply status was not "Success". - It only works on Windows OS. While cross-platform implementations of
Pingare available in .NET Core, they were removed from this project due to not understanding the build system that is required to handle them.
Download a release from the releases tab, or build SmartPing yourself. Include the appropriate DLL as a reference in your .NET project. Use classes in the SmartPing namespace instead of System.Net.NetworkInformation. Two DLLs are provided in releases.
- SmartPing.dll - Targets .NET Standard 2.0
- SmartPingF.dll - Targets .NET Framework 4.6.2