Results 1 to 6 of 6

Thread: Get Internet Connection Speed & Bytes Sent/Received

  1. #1

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    Question Get Internet Connection Speed & Bytes Sent/Received

    Irrespective of how a user connects to the internet (LAN/Modem/DSL etc.), this is how I am finding the connection speed & the amount of data sent & received but I don't think it gives the correct results especially the connection speed (Note: rtfNetStats is a RichTextBox control & tmrNetStats & tmrNetSpeed are 2 Timer controls):

    Code:
    Option Explicit
    Public Length       As Long
    Public seconds      As Long
    Private SelFunction As Byte
    
    Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" Alias "InternetGetConnectedStateExA" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Long, ByVal dwReserved As Long) As Long
    
    Public Enum EIGCInternetConnectionState
        INTERNET_CONNECTION_MODEM = &H1&
        INTERNET_CONNECTION_LAN = &H2&
        INTERNET_CONNECTION_PROXY = &H4&
        INTERNET_RAS_INSTALLED = &H10&
        INTERNET_CONNECTION_OFFLINE = &H20&
        INTERNET_CONNECTION_CONFIGURED = &H40&
    End Enum
    
    Public Property Get InternetConnected(Optional ByRef eConnectionInfo As EIGCInternetConnectionState, Optional ByRef sConnectionName As String) As Boolean
        Dim lR       As Long
        Dim iPos     As Long
        Dim dwFlags  As Long
        Dim sNameBuf As String
        
        sNameBuf = String$(513, 0)
        lR = InternetGetConnectedStateEx(dwFlags, sNameBuf, 512, 0&)
        eConnectionInfo = dwFlags
        iPos = InStr(sNameBuf, vbNullChar)
        If (iPos > 0) Then
            sConnectionName = Left$(sNameBuf, iPos - 1)
        ElseIf Not (sNameBuf = String$(513, 0)) Then
            sConnectionName = sNameBuf
        End If
        InternetConnected = (lR = 1)
    End Property
    
    Private Sub Form_Activate()
        Dim ConnSpeed  As Long
        Dim sMsg       As String
        Dim sName      As String
        Dim bConnected As Boolean
        Dim eR         As EIGCInternetConnectionState
        
        bConnected = InternetConnected(eR, sName)
    
        If ((eR And INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM) Then
            sMsg = sMsg & "Connection: Modem" & vbCrLf
        End If
        If ((eR And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN) Then
            sMsg = sMsg & "Connection: LAN" & vbCrLf
        End If
        If ((eR And INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY) Then
            sMsg = sMsg & "Connection: (via) Proxy" & vbCrLf
        End If
        If ((eR And INTERNET_CONNECTION_OFFLINE) = INTERNET_CONNECTION_OFFLINE) Then
            sMsg = sMsg & "Connection: Off-line" & vbCrLf
        End If
        If ((eR And INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED) Then
            sMsg = sMsg & "Configured: Yes" & vbCrLf
        Else
            sMsg = sMsg & "Configured: No" & vbCrLf
        End If
        If ((eR And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED) Then
            sMsg = sMsg & "System has RAS installed." & vbCrLf
        End If
       
        If (bConnected) Then
            rtfNetStats.Text = sMsg
        Else
            rtfNetStats.Text = "Not Connected: " & sName & vbCrLf & vbCrLf & sMsg
        End If
        
        Dim TCP As PMIB_TCPSTATS
        If (GetTcpStatistics(TCP) = NO_ERROR) Then
            rtfNetStats.Text = rtfNetStats.Text & vbCrLf & "Bytes Received: " & TCP.dwInSegs / 1000 & " KB"
            rtfNetStats.Text = rtfNetStats.Text & vbCrLf & "Bytes Sent: " & TCP.dwOutSegs / 1000 & " KB"
        Else
            MsgBox "Could not get TCP statistics!", vbExclamation, "Error!"
            tmrNetStats.Enabled = False
        End If
    
        SelFunction = 0
        tmrNetStats.Enabled = True
        seconds = 0
        tmrNetSpeed.Enabled = True
    
        Length = frmWebBrowser.wWeb.document.fileSize * 8
    
        If (seconds = 0) Then
            seconds = 1
        End If
    
        ConnSpeed = Round((Length / seconds) / 1024, 2)
        rtfNetStats.Text = rtfNetStats.Text & vbCrLf & vbCrLf & "Connection Speed: " & ConnSpeed & " kbps"
    End Sub
    
    Private Sub tmrNetSpeed_Timer()
        If (frmNetStats.Visible = True) Then
            seconds = seconds + 1
            tmrNetSpeed.Enabled = True
        End If
    End Sub
    I use a LAN connection to access the Internet but even when I am not connected to the Net, the above code never says that I am not connected to the Net. Also the connection speed usually hovers at the 1000 KBPS mark whereas the actual speed is far lesser than 1000 KBPS. Can someone please help me out with this?


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Get Internet Connection Speed & Bytes Sent/Received

    InternetGetConnectionStateEx returns True or False to indicate whether a connection is active, you should be checking the value of lR to determine if you're connected.

  3. #3

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    Re: Get Internet Connection Speed & Bytes Sent/Received

    lR always evaluates to 1 irrespective of whether I am connected to the Internet or not. Are you sure InternetGetConnectionStateEx will return either True or False?

    Any other ideas?


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Get Internet Connection Speed & Bytes Sent/Received

    if you disable your lan connection, lr will return 0,
    it checks if your computer has a connection, if you want to check if you are actually on the internet download a small file, you can use URLDownloadToFile api to test this
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    Re: Get Internet Connection Speed & Bytes Sent/Received

    Oh! Yeah....you are absolutely right, my dear friend....disabling the LAN connection does return 0. I will try out your suggestion as well.

    Another doubt please - my LAN connection is manually configured & it was I only who manually configured it but the following If...Else condition always says that it is not configured (executes the Else condition):

    Code:
    If ((eR And INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED) Then
        sMsg = sMsg & "Configured: Yes" & vbCrLf
    Else
        sMsg = sMsg & "Configured: No" & vbCrLf
    End If
    Any idea why so?


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Get Internet Connection Speed & Bytes Sent/Received

    my connection return is 18, which is ras and lan, never shows configured, so i have no idea what that means, when the connection is disabled, it just shows RAS

    possibly you can get a better connection return using internetCheckCheck connection, where you can specify an ip address, that of the service provider, should prove online or not

    i did some testing on connection speed, with varying results from about 70 - 107 kbs/sec for the same file from my isp providers server, so should be as quick as any other server, my connection speed is supposedly 1.5 mbs, but this is not the only computer using the connection
    Last edited by westconn1; Jan 12th, 2008 at 11:43 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width