Introduction
This program scans for open ports. You supply a host, e.g.: 192.168.1.1, hit Start, and the program scans.
Using the Code
Dim host As String
Dim port As Integer
Dim counter As Integer
Important Code
This is where most of the work is done. Next, we have a timer. As the timer ticks, we are going to try to connect to the port using a Try
/Catch
statement. If it connects, the port number is added to the second listbox. If it does not connect, listbox1
adds an item with the port number saying it is not open.
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
counter = counter + 1
TextBox2.Text = counter
host = TextBox1.Text
port = TextBox2.Text
Dim hostadd As System.Net.IPAddress = _
System.Net.Dns.GetHostEntry(host).AddressList(0)
Dim EPhost As New System.Net.IPEndPoint(hostadd, port)
Dim s As New System.Net.Sockets.Socket(_
System.Net.Sockets.AddressFamily.InterNetwork, _
System.Net.Sockets.SocketType.Stream, _
System.Net.Sockets.ProtocolType.Tcp)
Try
s.Connect(EPhost)
Catch
End Try
If Not s.Connected Then
ListBox1.Items.Add("Port " + port.ToString + " is not open")
Else
ListBox1.Items.Add("Port " + port.ToString + " is open")
ListBox2.Items.Add(port.ToString)
End If
Label3.Text = "Open Ports: " + ListBox2.Items.Count.ToString
End Sub
Start Button
The block of code below just starts the timer and the disabling/enabling buttons. Listbox1
adds text stating the host we are scanning.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add("Scanning: " + TextBox1.Text)
ListBox1.Items.Add("-------------------")
Button2.Enabled = True
Button1.Enabled = False
Timer1.Enabled = True
Timer1.Start()
End Sub
Form Load and Stop Button
Again, some simple code for enabling/disabling controls:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Button2.Enabled = False
TextBox2.Text = "0"
counter = 0
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Stop()
Timer1.Enabled = False
Button1.Enabled = True
Button2.Enabled = False
End Sub
Points of Interest
Take a look at the sockets part. Sockets are useful for many things when connecting over the internet, etc. This is my first article, I hope you like it.