What is a Port Scanner?
A port scanner is a tool used to identify which ports are open on a specific host.With the help of this tool you will be able to view the open ports of almost anything! So let's begin.
Code:
Public Class Form1
Dim Host As String
Dim Port As Integer
Dim Number As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Number = Number + 1 'Since the Number is set as 0 in the text box this will keeping adding another integer which will be dimmed as a port
TextBox2.Text = Number
Host = TextBox1.Text 'Where the Host IP will be placed
Port = TextBox2.Text 'The port being checked will be displayed
Dim hostadd As System.Net.IPAddress = System.Net.Dns.GetHostEntry(Host).AddressList(0) 'These couple of lines have to deal with validating the information found in the 2 textboxes above
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) 'Connection to said Host
Catch
End Try
If Not s.Connected Then 'Connection results goes to the appropriate box
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
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 'When a certain action is being completed the other buttons are not clickable hence Enabled = False
Button1.Enabled = False
Timer1.Enabled = True
Timer1.Start()
End Sub
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
End Class
With this code I also tweaked it a little bit to scan for a specific port and how I did that was through the following
Added a new time/buttonr and inserted this code into the timer
Code:
Number = Number 'It will only scan for this port
TextBox2.Text = Number
Host = TextBox1.Text 'Where the Host IP will be placed
Port = TextBox2.Text 'The port being checked will be displayed
'Now to set up the button
ListBox1.Items.Add("Scanning: " + TextBox1.Text)
ListBox1.Items.Add("-------------------")
Button2.Enabled = True 'When a certain action is being completed the other buttons are not clickable hence Enabled = False
Button1.Enabled = False
Timer1.Enabled = False
Button3.Enabled = False
Timer2.Stop() 'So you cdon't get the same result a million times
The rest of the code should remain intact. If there are any problems feel free to post here!