如何在C#中实现PsPing TCP ping [英] How to implement PsPing TCP ping in C#

查看:25
本文介绍了如何在C#中实现PsPing TCP ping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 C# 中实现 Mark Russinovich 的 SysInternals PsPing 工具使用 TCP ping 测量延迟.

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

我不确定它是如何进行 ping 调用的(显然不使用原始 Window 套接字,因为它不需要管理员权限才能运行).我知道 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 上的 ping,而不是 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]

没有预热和使用 TCP ping 的 psping 输出:

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

至于测量,我必须说,有时在不同的运行中会有很多不同的结果,但总体而言它们看起来非常接近:某种程度上证明测量 Connect() 调用已经足够好了.我在想,取几百个结果的中位数可能会更有把握地证明这一点.

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 ping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆