Question Retrieved bytes sent and received

pejalz

New member
Joined
May 28, 2008
Messages
3
Programming Experience
Beginner
hi everybody..

i am new user in this forum.I am a student and now i trying to develop my project course that to develop a system that will retrieve bytes send and received on my LAN with Function GetIfTable.

i have an error about this code..can anybody help me???
here i also attach my files..

thanks in advance for helping out of this problem
 

Attachments

  • my problem.zip
    22 KB · Views: 50
Upside: You can do this with NetworkInterface class of System.Net.NetworkInformation namespace, sample:
VB.NET:
'Imports System.Net.NetworkInformation

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each ni As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
        If Not ni.NetworkInterfaceType = NetworkInterfaceType.Loopback Then
            Me.i = ni
            Me.Text = "monitoring network interface: " & i.Description
            Timer1.Start()
            Exit For
        End If
    Next        
End Sub

Private i As NetworkInterface

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim stat As IPv4InterfaceStatistics = i.GetIPv4Statistics
    Me.lblRcvd.Text = "Bytes Received: " & (stat.BytesReceived \ 1024).ToString("n0") & " KB"
    Me.lblSent.Text = "Bytes Sent: " & (stat.BytesSent \ 1024).ToString("n0") & " KB"       
End Sub
Downside: you have to upgrade to at least .Net 2.0. (but there are many benefits :))
 
Thanks johnH...its great, but i don't know how to use .net 2.0 in vb.net 2003. i already installed .net framework 2.0 but the vb.net 2003 does not detect the .net 2.0. Is it the vb.net 2003 does not support framework 2.0? Please help me..i must develop this by using vb.net 2003.
 
Yes, you have to use VB 2005 or 2008, they are free for starters (Express) so no reason not give it a try.
 
Hi,
Thanks for that code. I'm using it to find and monitor active network conenctions with incoming traffic (atually, is there a way to assign BytesReceived to a variable, then set a delay timer of 2 seconds, then compare it and see if it increased?).

Anyway, the code works good in my application, except it crashes when I remove my USB modem connection (it is still "up" in connection status but bytesReceived stops)

When I took the code and isolated it in a seperate program, it crashes:

err.jpg



I'm a novice in VB, any ideas? Seems it has to do when BytesReceived = 0 on a "up" connection. :confused:

PS- I'm using VB2008 and .NET3.5
Imports System
Imports System.Net
Imports System.Net.NetworkInformation
Imports System.IO.Ports


Public Class Form1
Public i As NetworkInterface

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

For Each ni As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()


If ni.OperationalStatus = OperationalStatus.Up Then
Me.i = ni
Timer2.Start()
Exit For
End If

Next

End Sub




Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick

Dim stat As IPv4InterfaceStatistics = i.GetIPv4Statistics
TextBox1.Text = ": " & i.Description
Me.Label2.Text = "Bytes Received: " & (stat.BytesReceived \ 1024).ToString("n0") & " KB"
Me.Label1.Text = "Bytes Sent: " & (stat.BytesSent \ 1024).ToString("n0") & " KB"
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class
 
Last edited:
VB.Net has a language construct called Try-Catch that you can use to catch exceptions should they unexpectedly occur. Before using it it is best to evaluate the code for logic flaws and see if it is possible to easily validate values/objects used in a way that will prevent any unexpected errors to happen.

With an integrated network it is not possible for me to detach it and see what could happen where if I did that. The exception and screenshot you posted does not seem logical to me, it suggests "i" suddenly is Nothing, but it could go deeper. From your code I can only see that it must have been an instance some time before that, because you assigned it an instance just before you started Timer2 (Timer2 is presumably initially disabled).

A thing to remember here is that GetAllNetworkInterfaces method return an empty array if no interfaces is found, so your Timer1 will do absolutely nothing if that occurs. Here you have a chance to validate if all interfaces are gone and stop Timer2 that is still running. For example assign the returned array to a temporary variable and check its Length, which is 0 if it contains no items, else iterate that array. An even simpler solution is to stop Timer2 before each Timer1 iteration, that way Timer2 is only started again if an interface is found again - depending on your intervals you may or may not want to do this.

The "i.GetIPv4Statistics" call in Timer2 that threw can still do that even if you get Timer1 logic right, most likely this ticks more frequently and could happen between Timer1 ticks. This part can also be validated without resorting to Try-Catch, here you can check "If i Is Nothing" before you try to use this object.
 
Thanks JohnH, and greetings to Norway! :) I will have a look at that "catch" thing. Altho I have altered the code already to make it work in a different way.

Also, to identify the NetworkInterfaceType, I can get it to identify only one of the subsets:

Ethernet , Fddi , Loopback , Ppp , Slip , TokenRing , Unknown

Which is what's supposed to happen:
This property only returns a subset of the possible values defined in the NetworkInterfaceType enumeration
NetworkInterface.NetworkInterfaceType Property (System.Net.NetworkInformation)

How do I get it to identify one of the enumerated values?
NetworkInterfaceType Enumeration (System.Net.NetworkInformation)

Unknown
Ethernet
TokenRing
Fddi
BasicIsdn
PrimaryIsdn
Ppp
Loopback
Ethernet3Megabit
Slip
Atm
GenericModem Th
FastEthernetT
Isdn
FastEthernetFx
Wireless80211
AsymmetricDsl
RateAdaptDsl
SymmetricDsl
VeryHighSpeedDsl
IPOverAtm
GigabitEthernet
Tunnel
MultiRateSymmetricDsl
HighPerformanceSerialBus

They show an example, but it doesn't seem to work for me :confused:
I read in another forum something about WMI. Do I need to look more into that? Actually, I just want to identify WiFi adapters and seperate them from other connection types (and mybe return the SSID, if I can get that far :eek:)

Thanks again :)

The exception and screenshot you posted does not seem logical to me, it suggests "i" suddenly is Nothing, but it could go deeper. From your code I can only see that it must have been an instance some time before that, because you assigned it an instance just before you started Timer2 (Timer2 is presumably initially disabled)..
Both timers are "enabled" at start. Maybe thats the problem? I'll try disabling timer2 and see what happens....
 
Last edited:
Both timers are "enabled" at start. Maybe thats the problem?
If Timer2 ticks first that's a bullet-proof way to get the null-ref exception, because "i" variable hasn't been assigned yet.
 
Back
Top