I tried several approaches, at first thinking that I need to use raw sockets or at least use my own calls, but a simple TCP connection and closing seem to produce exactly the same results as the psping utility:
var times = new List<double>(); for (int i = 0; i < 4; i++) { var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Blocking = true; var stopwatch = new Stopwatch();
By checking traffic with Wireshark, I can confirm that both psping and the snippet above create exactly the same sequence of packets.
-> [SYN] <- [SYN,ACK] -> [ACK] -> [FIN,ACK] <- [FIN,ACK] -> [ACK]
Exiting psping without warming up and using TCP ping:
C:\>psping -w 0 stackoverflow.com:80 PsPing v2.01 - PsPing - ping, latency, bandwidth measurement utility Copyright (C) 2012-2014 Mark Russinovich Sysinternals - www.sysinternals.com TCP connect to 198.252.206.16:80: 4 iterations (warmup 0) connecting test: Connecting to 198.252.206.16:80: 92.30ms Connecting to 198.252.206.16:80: 83.16ms Connecting to 198.252.206.16:80: 83.29ms Connecting to 198.252.206.16:80: 82.98ms TCP connect statistics for 198.252.206.16:80: Sent = 4, Received = 4, Lost = 0 (0% loss), Minimum = 82.98ms, Maximum = 92.30ms, Average = 85.43ms
The output from the program above:
C:\>TcpPing.exe stackoverflow.com 80 88.60ms 83.65ms 84.05ms 84.05ms 83.65 88.60 85.09
As for measurements, I have to say that sometimes there are several different results on different runs, but in general they seemed pretty close: the view proves that the measurement of the Connect() call is good enough. I think the median of several hundred results can prove this with more certainty.
source share