Network Connection Monitor
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts
Page 1 of 1
Hello guys,
Today I took the time to write a tutorial about how to make a network connection monitor in VB.Net. To make this app was one of the challenges this month. I'm writing this tut so no one else has to face the problems I had when making this app. It was really frustrating for me. And that's exactly why I want to help others out.
Let's start with the beginning. Create a new VB.Net project and name it whatever you want, preferably "Connection Monitor" or "Connection Monitor".
Add a ComboBox to the the top of form and name it "CBNetInts". Naming the controls is up to you but I prefer these names 'cuz my code is according to them. Set the ComboBox's DropDownStyle property to DropDownList. This is to avoid accidentally editing the selected interface's name and hence crashing the app.
Next, add a Timer and name it "TmrStatus". Set the Enabled property to True and change the Interval property to 1. Smaller value means more rapid updates.
Add 6 Labels and change their Name and Text properties as follows:
LblStatus - "Status: "
IPAddEx - "External IP Address: "
IPAddIn - "Internal IP Address: "
SpdLbl - "Speed: "
DatSent - "Data Sent: "
DatRcvd - "Data Received: "
These are all the controls you'll be needing, 8 in total. In order to have an idea of the arrangement of the controls, refer to the attached image.
Now right-click on the form and press "View Code". Select all the text and delete it.
Before the Public Class Form1 add these two lines. By importing these namespaces, we won't have to type the complete terms every time we have to use them.
I hope you liked this tutorial and I hope you gain as much info from it as I did while researching the topic. Thanks for reading and don't forget to +rep!
Today I took the time to write a tutorial about how to make a network connection monitor in VB.Net. To make this app was one of the challenges this month. I'm writing this tut so no one else has to face the problems I had when making this app. It was really frustrating for me. And that's exactly why I want to help others out.
Let's start with the beginning. Create a new VB.Net project and name it whatever you want, preferably "Connection Monitor" or "Connection Monitor".
Add a ComboBox to the the top of form and name it "CBNetInts". Naming the controls is up to you but I prefer these names 'cuz my code is according to them. Set the ComboBox's DropDownStyle property to DropDownList. This is to avoid accidentally editing the selected interface's name and hence crashing the app.
Next, add a Timer and name it "TmrStatus". Set the Enabled property to True and change the Interval property to 1. Smaller value means more rapid updates.
Add 6 Labels and change their Name and Text properties as follows:
LblStatus - "Status: "
IPAddEx - "External IP Address: "
IPAddIn - "Internal IP Address: "
SpdLbl - "Speed: "
DatSent - "Data Sent: "
DatRcvd - "Data Received: "
These are all the controls you'll be needing, 8 in total. In order to have an idea of the arrangement of the controls, refer to the attached image.

Now right-click on the form and press "View Code". Select all the text and delete it.
Before the Public Class Form1 add these two lines. By importing these namespaces, we won't have to type the complete terms every time we have to use them.
Code: Select all
After Public Class Form1, insert the following two Dims. CurrentNetworkInterface is used to set the value of the selected interface when the ComboBox selection is changed and also to display the information accordingly. NetInts is used for all the interfaces on-board the computer.
Imports System.Net
Imports System.Net.NetworkInformation
Code: Select all
Once the Dims are done, we'll add two functions. One is to check if the interfaces to list are in accordance with the required type or not.
Dim CurrentNetworkInterface As NetworkInterface
Dim NetInts As NetworkInterface()
Code: Select all
The other function create a new list to which the required network interfaces will be added. It uses a filter to add only the specified types of interfaces to the list. You can add more filters if you want. If the interfaces fulfill the requirements of the filter, then they get added to the list.
Private Function ArrIntType(ByVal Arr As NetworkInterfaceType(), ByVal Match As NetworkInterfaceType) As Boolean
For Each IntType As NetworkInterfaceType In Arr
If IntType = Match Then
Return True
End If
Next
Return False
Code: Select all
Now we'll add a Sub which gets the data from the above functions and sets the data source of the ComboBox to it. By setting the DisplayMember property we are telling the ComboBox in what way it should display the items i.e. by their names, their IDs or their systemic name. Humans understand simplified names better, so that's what we're gonna use. Public Function GetNetInteface() As List(Of NetworkInterface)
Dim NetList As New List(Of NetworkInterface)
Dim NetInterfaceFilter As NetworkInterfaceType() = {NetworkInterfaceType.Ethernet, NetworkInterfaceType.Ppp, NetworkInterfaceType.Wireless80211}
For Each NetInt As NetworkInterface In NetInts
If ArrIntType(NetInterfaceFilter, NetInt.NetworkInterfaceType) Then
NetList.Add(NetInt)
End If
Next
Return NetList
End Function

Code: Select all
Now go back to the designer windows and double click on the form itself. This will show the code window and add the sub code for the Form_Load event. The first code we'll be adding to this event is to list the network interfaces in the ComboBox by calling the LoadCBNetInts sub.
Private Sub LoadCBNetInts()
NetInts = NetworkInterface.GetAllNetworkInterfaces
CBNetInts.DataSource = GetNetInteface()
CBNetInts.DisplayMember = "Name"
End Sub
Code: Select all
Next is the code for displaying the computer's internal IP address. It is the router's own address and only known on the same network. Each computer has several types of IP addresses, each having a ID. We'll use an integer to count the no. of addresses and fetch the second-last one, which I think is the one we need. At least it was the one I needed. Then we'll display it in the specified label.
LoadCBNetInts()
Code: Select all
Now we'll write the code for displaying the external IP address. This is the address that the internet uses to identify you or your computer. For this purpose we'll use the whatsmyip service through a WebClient. We'll link to the page which displays the IP address and download it as string. A typical address on that page is displayed as "document.write("--.---.--.-");". The downloaded string contains extra characters which we don't need, so we'll remove those chars by the String.Remove method and then display it in the label.
Dim IPAdds() As IPAddress
IPAdds = Dns.GetHostAddresses(System.Net.Dns.GetHostName)For i = 0 To IPAdds.Count - 2
IPAddIn.Text = "Internal IP Address: " & (IPAdds(i).ToString)
Next
Code: Select all
Go back to the designer window and double-click on the Timer. We'll use the Timer to check if the network is up or not and display the status in the label. If the network is accessible then we'll get the network speed and the amounts of data sent and received on the currently selected interface. The computer uses very small units like bits and bytes to display this info, which results in large numbers. We'll divide the speed by 1024 twice to get it in Mbits and we'll divide the data sent/received with 1024 once to get the values in kilobytes. One more thing, when we divide the numbers by 1024 we get the resultant values in fractions. To round them off to the nearest whole number, we use the Math.Truncate method.
Using Web As New WebClient
Dim s As String = Web.DownloadString("http://www.whatsmyip.us/showipsimple.php")
Dim l As String = s.Remove(0, 16)
Dim j As String = l.Remove(l.Length - 3, 3)
IPAddEx.Text = "External IP Address: " & j
End Using
Code: Select all
Now above the code window, below the tabs, there are ComboBoxes which allow you to choose the controls and their events for which we want to write the code. In the first box select the CBNetInts (ComboBox control) and in the events box select "SelectedIndexChanged". The basic code relative to the event will be added to the code window. Between that code, we'll write the code to set the current interface to the ComboBox's selected item.
If My.Computer.Network.IsAvailable = True Then
LblStatus.Text = "Status: Connected"
SpdLbl.Text = "Speed: " & Math.Truncate(CurrentNetworkInterface.Speed / 1024 / 1024) & " Mbits/s"
DatSent.Text = "Data Sent: " & Math.Truncate(CurrentNetworkInterface.GetIPv4Statistics.BytesSent / 1024) & " KB"
DatRcvd.Text = "Data Received: " & Math.Truncate(CurrentNetworkInterface.GetIPv4Statistics.BytesReceived / 1024) & " KB"
Else
LblStatus.Text = "Status: No Internet Access"
End If
Code: Select all
The coding's now done. So press the Start button on the top or go to Debug menu and press "Start Debugging". If all goes well, then you'll have the app running in front of you. Select the interface from the ComboBox which you are connected to and now you can see the related info.CurrentNetworkInterface = CBNetInts.SelectedItem
I hope you liked this tutorial and I hope you gain as much info from it as I did while researching the topic. Thanks for reading and don't forget to +rep!
Making a network monitor is probably one of the easiest and also useful program that can be made in .Net, and you did it quite well.
Your code is well organized and easy to read (though it wouldn't hurt to add some comments here and there).
Also i like that you use your own names for the controls, which is always a good practice.
Good job
Your code is well organized and easy to read (though it wouldn't hurt to add some comments here and there).
Also i like that you use your own names for the controls, which is always a good practice.
Good job

You can find me on Facebook or on Skype mihai_92b
Thanks #XTechVB I'm glad you liked it.
And it was frustrating for me because I couldn't get the code to work at all. In the first place, I was unable to load the items properly into the ComboBox. Secondly, I couldn't figure out how to set the CurrentInterface as it kept giving me the error that NetworkInterface cannot be converted into String. I fixed it by taking the Dim as a NetworkInterface instead of SelectedItem. So that did the trick. But it was quite frustrating as I had only 1 day to complete it and I badly wanted to do it because I knew I can.
About comments; I have them in the source code but I separated them myself to explain stuff better for the purpose of this tutorial. I've uploaded the source separately and each code is completely explained with comments.
viewtopic.php?f=71&t=11496
And it was frustrating for me because I couldn't get the code to work at all. In the first place, I was unable to load the items properly into the ComboBox. Secondly, I couldn't figure out how to set the CurrentInterface as it kept giving me the error that NetworkInterface cannot be converted into String. I fixed it by taking the Dim as a NetworkInterface instead of SelectedItem. So that did the trick. But it was quite frustrating as I had only 1 day to complete it and I badly wanted to do it because I knew I can.
About comments; I have them in the source code but I separated them myself to explain stuff better for the purpose of this tutorial. I've uploaded the source separately and each code is completely explained with comments.
viewtopic.php?f=71&t=11496
Great tutorial, and thanks for assembling all this info in one place. I remember while doing the challenge I had to look up several different sources to get what I wanted and it was really frustrating.
+rep cooll;
+rep cooll;
4 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023