Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to create a Bandwidth Monitor

timer

  • Please log in to reply
47 replies to this topic

#37 jammie999

jammie999

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 06 February 2009 - 07:11 AM

Hi i Keep getting a "runtime error 380. Invalid property value" and when i debug vb highlights the line

U9.Height = 1335 - DR(0) * 2

im still rather new to vb :-S
  • 0

#38 Sabotaz

Sabotaz

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 16 March 2009 - 08:09 AM

Tnx
  • 0

#39 apollophoebus

apollophoebus

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 30 March 2009 - 01:05 AM

nice
  • 0

#40 ronfaever

ronfaever

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 07 April 2009 - 02:00 AM

Please, can you help me with VB codes for a bandwidth analyzer? I urgently need it for my project. Thanks very much.
  • 0

#41 hdbbdh

hdbbdh

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 14 June 2009 - 09:05 AM

Thank you man
Realy it is a good Idea

Thank you again
  • 0

#42 181jenkins

181jenkins

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 14 June 2009 - 05:56 PM

Woot nice ima try it
  • 0

#43 flipped

flipped

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 12 July 2009 - 10:27 AM

:) thanks
  • 0

#44 Dohment

Dohment

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 12 August 2009 - 10:38 PM

Interesting, thanks for this.
  • 0

#45 milo0309

milo0309

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 15 August 2009 - 02:02 AM

Thank you very much this code was of great help 2 me
  • 0

#46 blindtrevor

blindtrevor

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 16 September 2010 - 05:10 AM

Hi,

There is a slightly neater way of getting the NetStat -e as a variable without having to constantly write and read to the disk (keep everything in memory)

Microsoft aren't really that fond of using the Shell command... instead they like you to use a process and pass the results through to a variable.

The way to do this in this instance is like so:

First create a new form with a timer (timer1) set to 1000 interval, a text box (TextBox1) set to multiline, a button (Button1) and a label (Label1).

Now you have that we can put the code in:

'----------------------------------------------------------------------------------------------------------------------------
'Script Name : BandwidthMonitor.sln
'Author      : Andrew Samuel
'Created     : 16/09/2010
'Description : Tracks the inbound and outbound traffic on a PC
'----------------------------------------------------------------------------------------------------------------------------
'We need to import System.Math to allow a bit of rounding
'----------------------------------------------------------------------------------------------------------------------------
Imports System.Math

Public Class Form1
    '----------------------------------------------------------------------------------------------------------------------------
    'Here we declare all our variables
    '----------------------------------------------------------------------------------------------------------------------------
    Dim objProcess As New Process               'Used to process the netstat command
    Dim strOutput As String                     'A string to store the output of the netstat process
    Dim strError As String                      'A string to store any errors of the netstat process
    Dim DLoad As Long                           'This will be the raw download data from netstat
    Dim ULoad As Long                           'This will be the raw upload data from netstat
    Dim LastULoad As Long = 0                   'This will be used to calculate the surrent usage
    Dim LastDLoad As Long = 0                   'This will be used to calculate the current usage
    Dim SString As String()                     'We need this string array to give us the ability to get the data we need from the netstat command
    Dim doAfterSecondPass As Boolean = False    'This we will use to allow the program to process something on and after the second pass through

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Start()                          'This will start polling the netstat each second
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'This is where the fun starts!
        '
        'We need to set all the variables inside the objProcess
        objProcess.StartInfo.RedirectStandardOutput = True
        objProcess.StartInfo.CreateNoWindow = True
        objProcess.StartInfo.RedirectStandardError = True
        objProcess.StartInfo.FileName() = "netstat"
        objProcess.StartInfo.Arguments() = "-e"
        objProcess.StartInfo.UseShellExecute = False
        'Now we will start the process
        objProcess.Start()
        'Here we will store the outputs to our 2 strings
        strOutput = objProcess.StandardOutput.ReadToEnd
        strError = objProcess.StandardError.ReadToEnd
        'And then we wait for the process to end before carrying on
        objProcess.WaitForExit()
        'Now we output the information we have
        TextBox1.Text = strOutput 'This is the raw data from netstat -e
        SString = Split(strOutput, "Bytes") 'We split that string at the word Bytes
        DLoad = Int(Mid(SString(1), 14, 17)) 'Find the download figure and store it to DLoad variable
        ULoad = Int(Mid(SString(1), 31, 16)) 'Find the upload figure and store it to ULoad variable

        'Here is where we work out how much data has passed in the last second
        If doAfterSecondPass = True Then
            Label1.Text = "Upload: " & roundBytes(ULoad - LastULoad) & "/s" & vbCrLf & "Download: " & roundBytes(DLoad - LastDLoad) & "/s"
        Else
            doAfterSecondPass = True
        End If
        'And this is where we set the las values ready to use next time
        LastDLoad = DLoad
        LastULoad = ULoad
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    'This is the function to return a slightly friendlier value for the bytes we got from netstat
    '----------------------------------------------------------------------------------------------------------------------------
    Function roundBytes(ByVal bytes As String)
        If bytes > 1073741824 Then
            Return Round(bytes / 1073741824, 2) & " GB"
        ElseIf bytes > 1048576 Then
            Return Round(bytes / 1048576, 2) & " MB"
        ElseIf bytes > 1024 Then
            Return Round(bytes / 1024, 2) & " KB"
        ElseIf bytes = 0 Then
            Return bytes & " B"
        Else
            Return bytes & " B"
        End If
    End Function

End Class

I've tried to comment as much as I could.

I hope this is helpful to someone.

Attached Files


  • 0

#47 Jarryd

Jarryd

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 63 posts
  • Location:Australia
  • Programming Language:C, Java, C++, C#

Posted 17 September 2010 - 03:57 AM

Thanks mate,

This thread is very interesting :D
  • 0

#48 blindtrevor

blindtrevor

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 20 September 2010 - 08:13 AM

I've found an even better way of doing this - less glitchy and true .NET:

Imports System.Net.NetworkInformation
Then replace the whole objProcess bit with this:
Try
            Dim netInterface As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces
            For Each net As NetworkInterface In netInterface
                Dim statistics As IPv4InterfaceStatistics = net.GetIPv4Statistics
                If net.Name <> "MS TCP Loopback interface" Then
                    If net.OperationalStatus = 1 Then
                        DLoad = statistics.BytesReceived
                        ULoad = statistics.BytesSent
                    End If
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, ex.InnerException)
        End Try

  • 0





Also tagged with one or more of these keywords: timer

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download