This is a mirror of official site: http://jasper-net.blogspot.com/

Quick and dirty network usage meter with C#

| Tuesday, April 6, 2010
I have always been doubtful of my billing by my internet providers. I wanted to write some code that could help me get a rough estimate of bandwidth I was using in every session. In this article, I will show you how to create a very basic network usage meter for serial port/USB/mobile phone modems with only C# (no Win API calls, sockets or fancy stuff).


The network meter's icon (one with yellow dot).
Note that network icon status.
The icon's tooltip shows usage.
Note that network icon status.

Some entries from the log file.
The last entry corresponds to reported usage.
Clicking on 'Exit' menu item will close the program.
My program is not as sophisticated as others available on the internet, but it does work well for me. It shows usage in current session and logs it in a file. This code should work with many USB modems which actually setup a COM port for communication (example GPRS/3G/HSIA data cards or mobile phones being used as modems).

The basics
The reason, I call this code 'dirty' is, it not well tested and was written in hurry. The program revolves around only two concepts:

   * If data usage is available, then read it from performance counters.
   * If not, then assume that the network is down and wait for network to be available using the 'System.Net.NetworkInformation.NetworkChange' class.

My program has only been tested with HSIA modem, which gets connected to a virtual COM4 port. The program is written in such a way that it handles frequent connection/disconnection and tries to log usage in every session.

The Code
This is a winforms application. In the constructor of the Form we :

   * Create two performance counters, one for measuring downloads and another for measuring uploads.
   * A Timer to poll the performance counters, so that we can check if they are readable (network/port is available).
   * Attach event handler to 'NetworkAvailabilityChanged' event of 'NetworkChange' class, so that we are updated when the COM4 port is available.

The code is given below:

static PerformanceCounter dataSentCounter;
       static PerformanceCounter dataReceivedCounter;
       System.Timers.Timer  networkMonitor;
       string category, instance, fileName;
       static float u, d;

       public Form1()
       {
            InitializeComponent();
            category = ConfigurationSettings.AppSettings["Category"];
            instance = ConfigurationSettings.AppSettings["Instance"];
            fileName = ConfigurationSettings.AppSettings["FilePath"];
            dataSentCounter = new PerformanceCounter(category, "Bytes Transmitted", instance);
            dataReceivedCounter = new PerformanceCounter(category, "Bytes Received", instance);
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
            networkMonitor = new System.Timers.Timer();
            networkMonitor.Interval = Int32.Parse(ConfigurationSettings.AppSettings["NetworkPollInterval"]);
            networkMonitor.Elapsed += new ElapsedEventHandler(networkMonitor_Elapsed);        
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           try
           {
               u = dataSentCounter.NextValue();
               d = dataReceivedCounter.NextValue();
           }
           catch
           { return; }
           networkMonitor.Start();
       }


Read more: ashishware.com

Posted via email from jasper22's posterous

0 comments: