首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何监控具体应用的网络带宽使用情况?

如何监控具体应用的网络带宽使用情况?
EN

Stack Overflow用户
提问于 2014-12-10 16:56:43
回答 3查看 10.3K关注 0票数 5

我正在尝试学习如何监控特定应用程序的网络带宽使用情况。我正在查看IPv4InterfaceStatistics,但它似乎可以监控NIC卡的性能。

我想监控一个特定的应用程序,看看每秒消耗了多少带宽。

有谁知道如何做到这一点的例子吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-11 16:50:29

代码语言:javascript
复制
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;
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2021-01-05 22:31:58

以下是使用PerformanceCounter的修改版本:

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2014-12-10 17:01:04

如果您熟悉开放系统互连模型http://en.wikipedia.org/wiki/OSI_model,您会看到您尝试与第3层交互,而您本应与第7层交互。

如果你与你使用的任何类的连接能够测量发送的字节,特别是如果你正在传输单个字节,(这是因为我不知道你的代码看起来是什么样子),你应该能够计算出一段时间内的字节数除以秒数,你就会得到结果。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27396786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档