问题描述
我想实现在C#马克Russinovich的Sysinternals的使用TCP的ping来测量延迟。
I am trying to implement Mark Russinovich's SysInternals PsPing tool in C# to measure latencies using TCP ping.
我不知道它是如何使ping呼叫(显然不是使用原始窗口插座,因为它不需要管理员权限才能运行) 。我知道 hping
通过TCP发送SYN数据包,并测量响应时间。
I am not sure how it makes the ping call (apparently not uses raw Window sockets, because it does not require Administrator privileges to run). I know hping
sends SYN packet over TCP and measures response time.
什么是用于精确测量的实现技术其中通过TCP不会测量页面加载时间,但只是网络延迟的包确认服务器延迟?是否有此库
What would be the implementation technique for accurate measurement of server latency which over TCP does not measure page load times but just the network latency for the package acknowledgement? 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
更新:请不要回答诸如你为什么不使用Ping类,它已经做的事,我想咨询一下平通过TCP,ICMP没有。
Update: Please don't answer like "why don't you use Ping class, it already does the thing" as I'm asking about ping over TCP, not ICMP.
推荐答案
我尝试了好几种方法,第一个想法,我不得不使用原始套接字或至少使用本地电话,但一个简单的TCP连接和关闭似乎产生了完全相同的结果作为psping实用程序:
I have tried several approaches, first thinking I had to use raw sockets or at least use native calls, but a simple TCP connect and close seems to create 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());
检查使用Wireshark的交通,我可以证实这两个psping和上面的代码段创建完全一样的序列。包
Inspecting the traffic using Wireshark, I can confirm both psping and the snippet above are creating exactly the same sequence of packets.
-> [SYN]
<- [SYN,ACK]
-> [ACK]
-> [FIN,ACK]
<- [FIN,ACK]
-> [ACK]
这是没有热身psping和使用TCP平输出:
Output from psping with no warm-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
从上面的程序输出:
C:\>TcpPing.exe stackoverflow.com 80
88.60ms
83.65ms
84.05ms
84.05ms
83.65 88.60 85.09
至于测量,我必须说,有时有不同的运行相当多的不同的结果,但总体来说他们似乎非常接近:一种证明测量连接()
通话是不够好。我在想,走几百结果的中位可能会多一点信心,证明了这一点。
As for measurements, I must say, sometimes there are quite a few different results at different runs, but overall they seemed pretty close: kind of proves measuring the Connect()
call is good enough. I'm thinking, taking a median of a few hundred results might prove it with a bit more confidence.
这篇关于如何实现在C#中PsPing TCP平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!