如何监控特定应用程序的网络带宽使用情况?

5

我正在尝试学习如何监控特定应用程序的网络带宽使用情况。我正在查看IPv4InterfaceStatistics,但似乎这只能监视网卡的性能。
我想监视特定应用程序,以查看每秒消耗了多少带宽。
是否有人知道如何实现这一点的示例?


Windows 资源监视器。 - Deepak Mishra
1
@DeepakMishra 我认为他想以编程方式解决这个问题,因为它涉及到C#和堆栈,而不是服务器故障,但我可能错了。 - Magic-Mouse
是的,我想以编程方式解决这个问题。 - Manish Jain
https://dev59.com/QXRC5IYBdhLWcg3wAcM3#442459 - Deepak Mishra
@Manish:如果那个不起作用,请告诉我。 - Deepak Mishra
显示剩余4条评论
3个回答

3
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;
        }
    }
}

1

这是一个使用 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);

-2

如果您熟悉OSI模型http://en.wikipedia.org/wiki/OSI_model,您会发现您试图与第3层交互,而应该与第7层交互。

在您使用的任何类中,您可以测量发送的字节数,特别是如果您正在传输单个字节(这是因为我不知道您的代码是什么样子),您应该能够计算一段时间内的字节数,除以秒数,然后您就有了结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接