Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the "Latency" of a process that has a TCP connection open?

Tags:

c#

networking

I am looking to get the "Latency" field of a TCP connection. I notice windows Resource Monitor has this field, and I was wondering if there was a way I can find it. Preferrably without using WMI.

If you are unsure what field I am talking about, open Task Manager, goto the Performance tab and hit the Resource Monitor button.

Once Resource Monitor is open, expand the TCP Connections area and you will see a Latency field. Is there anyway to access this programatically?

Thanks!

like image 694
Dave Avatar asked Jun 16 '10 17:06

Dave


1 Answers

I'm assuming that the Resource monitor looks at the Round Trip Time (RTT) for a given TCP table entry. This gives a reasonable indication of overall network delay.

There is an API you can use to access these statistics, namely GetPerTcpConnectionEStats. This allows you to retrieve plenty of stats about a specific TCP connection.

You basically get the list of tcp connections first using GetTcpTable, then find the row you want, and pass it to GetPerTcpConnectionEStats, with the TcpConnectionEstatsPath as the EstatsType parameter, so that you should get a TCP_ESTATS_PATH_ROD_v0 structure.

This structure has a number of RTT stats in it, the most useful of which is probably the SumRtt and CountRtt members, which you could use to get an average RTT calculation for that particular TCP table row.

Note that these functions only exist in Vista and above, but then so does the Resource Monitor, so I reckon that's ok.

like image 149
Alistair Evans Avatar answered Oct 28 '22 13:10

Alistair Evans