How to implement PSPing TCP ping in C #

I am trying to implement Mark Russinovich SysInternals PsPing tool in C # to measure latency using TCP ping.

I'm not sure how the ping call does it (apparently, it does not use Windows raw sockets, since it does not require administrator privileges). I know that hping sends a SYN packet over TCP and measures the response time.

What will be the method for implementing accurate measurement of server latency, which does not measure page load time over TCP, but only the network timeout to confirm the packet? Is there a library for this?

 C:\>psping stackoverflow.com:80 PsPing v2.01 - PsPing - ping, latency, bandwidth measurement utility Copyright (C) 2012-2014 Mark Russinovich Sysinternals - www.sysinternals.com Pinging 198.252.206.16 with 32 bytes of data: 5 iterations (warmup 1) ping test: Reply from 198.252.206.16: 81.57ms Reply from 198.252.206.16: 80.81ms Reply from 198.252.206.16: 80.68ms Reply from 198.252.206.16: 80.52ms Reply from 198.252.206.16: 80.71ms Ping statistics for 198.252.206.16: Sent = 4, Received = 4, Lost = 0 (0% loss), Minimum = 80.52ms, Maximum = 80.81ms, Average = 80.68ms 

Update: Please do not answer like this: โ€œWhy donโ€™t you use the Ping class, it already does thisโ€, since I ask for ping over TCP, not ICMP.

+5
source share
2 answers

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(); // Measure the Connect call only stopwatch.Start(); sock.Connect(endPoint); stopwatch.Stop(); double t = stopwatch.Elapsed.TotalMilliseconds; Console.WriteLine("{0:0.00}ms", t); times.Add(t); sock.Close(); Thread.Sleep(1000); } Console.WriteLine("{0:0.00} {1:0.00} {2:0.00}", times.Min(), times.Max(), times.Average()); 

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.

+14
source

Winsock will certainly allow you to do this easily.

Have you looked at source programs like http://www.elifulkerson.com/projects/tcping.php ?

A fairly simple program (console) that does exactly what you want (AFAIK), and comes with source code that looks very clear, short and easy to read (even for programmers not using C ++, I did not practice C + + after a while, and despite this, I was very pleased to read).

You can create it and debug it using VS to quickly find what you want. It should be easy to get a few Win32 API calls involved in TCP Ping there. Thus, you can easily convert the interesting part to C # or paste it into a managed DLL.

Just do the test if it does exactly what you want.

Source link: http://www.elifulkerson.com/projects/downloads/tcping-0.23/tcping-src.zip

+1
source

Source: https://habr.com/ru/post/1203506/


All Articles