Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor the network bandwidth usage of a specific application?

I am trying to learn how to monitor the network bandwidth usage of a specific application. I am looking at IPv4InterfaceStatistics, but that seems to monitor an NIC card's performance.
I'd like to monitor a specific application to see how much bandwidth is consumed every second.
Does anyone know of an example of how this can be done?

like image 458
Manish Jain Avatar asked Nov 09 '22 22:11

Manish Jain


2 Answers

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;
        }
    }
}
like image 130
Deepak Mishra Avatar answered Nov 14 '22 22:11

Deepak Mishra


Here is a modified version using 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);
like image 31
TQD Avatar answered Nov 14 '22 23:11

TQD