如何监控特定应用程序的网络带宽使用情况?
Posted
技术标签:
【中文标题】如何监控特定应用程序的网络带宽使用情况?【英文标题】:How to monitor the network bandwidth usage of a specific application? 【发布时间】:2014-12-10 08:56:42 【问题描述】:我正在尝试学习如何监控特定应用程序的网络带宽使用情况。我正在查看IPv4InterfaceStatistics
,但这似乎是在监控 NIC 卡的性能。
我想监控一个特定的应用程序,看看每秒消耗了多少带宽。
有谁知道如何做到这一点的例子?
【问题讨论】:
Windows 资源监视器。 @DeepakMishra 我想他想以编程方式解决这个问题,因为它是 C# 和堆栈,而不是服务器故障,但我可能是错的。 是的,我想以编程方式解决这个问题 ***.com/questions/442409/calculating-bandwidth/… 网络性能计数器msdn.microsoft.com/en-us/library/70xadeyt%28v=vs.110%29.aspx 【参考方案1】:using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
class Program
static void Main(string[] args)
while (true)
var bytesSentPerformanceCounter = new PerformanceCounter();
bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking";
bytesSentPerformanceCounter.CounterName = "Bytes Sent";
bytesSentPerformanceCounter.InstanceName = GetInstanceName();
bytesSentPerformanceCounter.ReadOnly = true;
var bytesReceivedPerformanceCounter = new PerformanceCounter();
bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking";
bytesReceivedPerformanceCounter.CounterName = "Bytes Received";
bytesReceivedPerformanceCounter.InstanceName = GetInstanceName();
bytesReceivedPerformanceCounter.ReadOnly = true;
Console.WriteLine("Bytes sent: 0", bytesSentPerformanceCounter.RawValue);
Console.WriteLine("Bytes received: 0", bytesReceivedPerformanceCounter.RawValue);
Thread.Sleep(1000);
private static string GetInstanceName()
string returnvalue = "not found";
//Checks bandwidth usage for CUPC.exe..Change it with your application Name
string applicationName = "CUPC";
PerformanceCounterCategory[] Array = PerformanceCounterCategory.GetCategories();
for (int i = 0; i < Array.Length; i++)
if (Array[i].CategoryName.Contains(".NET CLR Networking"))
foreach (var item in Array[i].GetInstanceNames())
if (item.ToLower().Contains(applicationName.ToString().ToLower()))
returnvalue = item;
return returnvalue;
【讨论】:
【参考方案2】:这是使用 PerformanceCounter 的修改版本:
var processFileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
var bytesReceivedPerformanceCounter = new PerformanceCounter();
bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
bytesReceivedPerformanceCounter.CounterName = "Bytes Received";
bytesReceivedPerformanceCounter.InstanceName = VersioningHelper.MakeVersionSafeName(processFileName, ResourceScope.Machine, ResourceScope.AppDomain);
bytesReceivedPerformanceCounter.ReadOnly = true;
Console.WriteLine("Bytes received: 0", bytesReceivedPerformanceCounter.RawValue);
【讨论】:
【参考方案3】:如果您熟悉 OSI 模型 http://en.wikipedia.org/wiki/OSI_model,您会发现您尝试与第 3 层交互,而您应该与第 7 层交互。
我与您使用的任何类的连接都可以测量发送的字节数,特别是如果您正在传输单个字节,(这是因为我不知道您的代码看起来如何),您应该能够计算一个时间除以秒数的字节数,你就会得到你的结果。
【讨论】:
以上是关于如何监控特定应用程序的网络带宽使用情况?的主要内容,如果未能解决你的问题,请参考以下文章