+ Reply to Thread
Page 1 of 5 123 ... LastLast
Results 1 to 10 of 48

Thread: How to create a Bandwidth Monitor

  1. #1
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    How to create a Bandwidth Monitor

    As we know the bandwidth is measured in bits per second. A Bandwidth monitor is a kind of software which displays the upload and download rate and sometimes it shows the load on a graph too. And that’s what we are going to create today.

    But HOW?
    There is a Windows tool called netstat which displays network statistics, some of them needed by us now. If you run the command prompt and type netstst –e you will see how much you have downloaded and uploaded since you have started your PC.

    But HOW ARE WE GOING TO CALCULATE THE BANDWIDTH?
    Very simple! As we said bandwidth is measured in bits per second so every second we will discount the result we got 1 second ago to the new result so we will gt how much we have downloaded(or uploaded) for a second. We will make a timer do this every second.

    Now HOW TO CODE IT?
    For this tutorial I created a module that we will use to get the current download and upload amount.

    First I have declared 2 public variables DLoad and ULoad. Both are declared as Long because they will hold large numbers. DLoad will hold the amount of downloaded stuff in bytes until now and ULoad the upload.

    Then we have a function called GetLoadInfo() which will fill the variables above. In this function first we see 2 variables and this line:

    Code:
    Shell "cmd.exe /C netstat -e> " & Chr(34) & App.Path & "\Load.tmp" & Chr(34), vbHide
    This means that netstat –e will be called and the results will be stored in the file Load.tmp in the application path.

    After this line we have some other code:

    Code:
    On Error GoTo PassRead
    Open App.Path & "\Load.tmp" For Input As #1
    
        Do Until EOF(1)
            Input #1, TempLoadStr
            LoadStr = LoadStr & TempLoadStr
        Loop
    
    Close #1
    The 2 variables will be used to read what is stored in the netstat’s results. After this, the results will get split. This is done in the next lines:

    Code:
    SString = Split(LoadStr, "Bytes")
    
    DLoad = Int(Mid(SString(1), 14, 17))
    ULoad = Int(Mid(SString(1), 31, 16))
    We noticed the On Error GoTo PassRead line above. That will be used to pass the code exestuation if netstat hasn’t shown any results.

    Now the whole module looks like this:

    Code:
    Public ULoad As Long
    Public DLoad As Long
    
    Public Sub GetLoadInfo()
    Dim TempLoadStr As String
    Dim LoadStr As String
    
    Shell "cmd.exe /C netstat -e> " & Chr(34) & App.Path & "\Load.tmp" & Chr(34), vbHide
    
    On Error GoTo PassRead
    Open App.Path & "\Load.tmp" For Input As #1
    
        Do Until EOF(1)
            Input #1, TempLoadStr
            LoadStr = LoadStr & TempLoadStr
        Loop
    
    Close #1
    
    SString = Split(LoadStr, "Bytes")
    
    DLoad = Int(Mid(SString(1), 14, 17))
    ULoad = Int(Mid(SString(1), 31, 16))
    
    PassRead:
    End Sub

    Now we can create the form…

    In the form first we declare 3 variables, LastULoad, LastDLoad, SeccondsPassed all of them as Long, then we create 2 labels, lblDC and lblUC (label Download Container and label Upload Container), we add a timer, we set the timer’s interval to 1000 milliseconds and add this code to the timer:

    Code:
    Private Sub Timer1_Timer()
    Call GetLoadInfo
    
    If SecondsPassed > 3 Then
        lblDC.Caption = Int((DLoad - LastDLoad) / 1000) * 8 & "kb/s"
        lblUC.Caption = Int((ULoad - LastULoad) / 1000) * 8 & "kb/s"
    End If
    
    SecondsPassed = SecondsPassed + 1
    
    LastDLoad = DLoad
    LastULoad = ULoad
    End Sub
    As we see, first we call the function from the module, then, we see a variable that needs to be 3 if we want to show the results. The reason is that we will need about 3 seconds to calibrate with the netstat’s results. If we wouldn’t do that, at first we would get extreme bandwidth results so we wait 3 seconds for calibration. Then we fill the labels. We see that the results are being multiplied and divided by some numbers. Dividing by 1000 means we don’t want to show the results in bytes, but in kilo bytes and multiplying by 8 means we want to see the results in bits, not bytes.

    After this code we see that the LastDLoad and LastULoad variables are being filled with new values, getting ready for the next calculation.

    This is all… If anything doesn’t work, download the attachment and find the errors. In the attachment the Bandwidth Meter is much advanced, it has graphs included to show the download and upload rates.

    Here's a screenshot of CodeCall's Bandwidth Monitor included in the attachment:



    If you have any questions or suggestions please post them here.

    Best wishes,
    Dren
    Attached Files Attached Files

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: How to create a Bandwidth Monitor

    As promised, Mr. Dren, +rep!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    Re: How to create a Bandwidth Monitor

    Thank you BRO So... U like this thread right?

  5. #4
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: How to create a Bandwidth Monitor

    I didn't even read it.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #5
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    Re: How to create a Bandwidth Monitor

    looooool It's not worth it, trust me lol

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: How to create a Bandwidth Monitor

    I saw the title and groaned.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #7
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    Re: How to create a Bandwidth Monitor

    I decided to create this tutorial because I usually check up Google if something like that exists. This didn't, so I know that if someone will search for something like this in Google it this will be displayed the first so we get more Users in CodeCall

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: How to create a Bandwidth Monitor

    Good idea!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: How to create a Bandwidth Monitor

    Really nice thread
    thnx dren, i always wanted to use dos commands in my app but i didnt know how to store results into variables
    thnx again

  11. #10
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    Re: How to create a Bandwidth Monitor

    Quote Originally Posted by amrosama View Post
    Really nice thread
    thnx dren, i always wanted to use dos commands in my app but i didnt know how to store results into variables
    thnx again
    Thank you amrosama! For what you need you can also use the DosOutputs class. Using that you can put your results directly in a variable without needing to write them in a file first. So, why not give me some +rep :$

+ Reply to Thread
Page 1 of 5 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Turn Monitor Off
    By LogicKills in forum Classes and Code Snippets
    Replies: 11
    Last Post: 03-26-2011, 12:56 AM
  2. Replies: 0
    Last Post: 07-23-2010, 03:41 AM
  3. TASM information of Monitor! Help me plz!
    By Vic in forum Assembly
    Replies: 0
    Last Post: 05-11-2010, 03:35 PM
  4. java monitor
    By incognitoff in forum Java Help
    Replies: 1
    Last Post: 03-02-2008, 02:48 PM
  5. Monitor UpGrade
    By xXHalfSliceXx in forum Computer Hardware
    Replies: 26
    Last Post: 01-02-2007, 06:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts