My Professional Profile

I am Johnson Augustine Sr.Software Engineer and System Architect. I have 10 Yrs of hands on expertise in ASP.net MVC 5 , Angular 5 , C# MVC Razor, WPF MVVM , Android , IOS Swift 3 , IOS11 MSSQL,MySQL Database,,PHP,C/C++/Visual C++/G++/QT++,Com,DirectX,Open CV,EMGU CV , embedded System Development , [Raspberry PI]. ,html,Javascript,Jquery,Ajax.CSS , Networking ,Cyber security, Ethical Hacking You can see my professional profile at http://linkedin.com/in/johnsontaugustine Email : johnsonaugustine@live.com

Saturday, 29 June 2013

How to find network traffic of a specific or currently active network adapter using c# windows


How to find network traffic of a specific or currently active network adapter using c# windows

To do this First import the name spaces

using System.Net.NetworkInformation;
using System.Diagnostics;

Delcare global varibales

 #region BandwidthVariables
        private const double timerUpdate = 1000;
        private NetworkInterface[] nicArr;
        private Timer timer;
        double BytesRecived=0;
        #endregion


 // Edited from here 
        #region Network
        /// <summary>
        /// Initialize the Timer 
        /// </summary>

        private void InitializeTimer()
        {
            timer = new Timer();
            timer.Interval = (int)timerUpdate;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        private void InitializeNetworkInterface()
        {
            // Grab all local interfaces to this computer
            nicArr = NetworkInterface.GetAllNetworkInterfaces();
       }

        /// <summary>
        /// The Timer event for each Tick (second) to update the UI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer_Tick(object sender, EventArgs e)
        {
            UpdateNetworkInterface();
        }

        /// <summary>
        /// Update GUI components for the network interfaces
        /// </summary>
        private void UpdateNetworkInterface()
        {
            // Grab NetworkInterface object that describes the current interface
            NetworkInterface nic = nicArr[0];

            for (int i = 0; i < nicArr.Length; i++)
            {
                if (nicArr[i].OperationalStatus == OperationalStatus.Up) // Checking for active NIC
                {
                    nic = nicArr[i];
                  
                    break;
                    //nic is attached to some form of network
                }

            }
            // Grab the stats for that interface
            IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();
            int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - BytesRecived) / 1024;
            BytesRecived = interfaceStats.BytesReceived; 
            lblBandwidth.Text = bytesReceivedSpeed.ToString() + " KB/s";

// Put a label and timer in your form and name the label as "lblBandwidth"

        }
        #endregion

No comments:

Post a Comment

Note: only a member of this blog may post a comment.