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?
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.
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?
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
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?
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