Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'CODE WRITTEN BY FREKVENS1, www.Frekvens1.webs.com.
- 'THIS CODE WAS WRITTEN FOR 'DSW Tutorials' AS ANOTHER WAY TO DO THE SAME TASK BETTER. CREDITS TO HIM/HER/THEM FOR MAKING THE VIDEO!
- 'https://www.youtube.com/watch?v=5qpn0vxKr6c'
- 'Feel free to use the code at own will, and if you need help understanding it better give me a call!
- 'This is not the optimal code, but instead used to learn more.
- 'Thanks for watching my code, and I hope you find something useful in here! (VB.NET).
- Imports System.Threading
- Imports System.Net.Sockets
- Public Class Form1
- WithEvents checkPort As ScanPort
- Private newPort As String, Working As Boolean
- Private Sub btnScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScan.Click
- If Working Then
- MsgBox("[ERROR] Scan is still running!", MsgBoxStyle.Critical)
- Else
- Try
- lbPorts.Items.Clear() 'Clears the listbox for a new scan.
- Working = True
- Dim t As New Thread(AddressOf startScan) 'Lag preventer.
- t.Start()
- Catch ex As Exception
- Working = False
- End Try
- End If
- End Sub
- Private Sub startScan() 'Just to make the UI (User Interface (Main Thread) ) less 'laggy'.
- checkPorts(txtIP.Text, nudStart.Value, nudStop.Value) 'Starts the port scanner, checking each of the desired ports.
- End Sub
- Private Sub checkPorts(ByVal IPaddress As String, ByVal PortRangeStart As Integer, ByVal PortRangeMax As Integer)
- Working = True
- Try
- For Port = PortRangeStart To PortRangeMax 'Increases the port value by 1 until it reaches the max value, starting from the start value.
- Thread.Sleep(20) 'Not needed, but keeps the software from freezing.
- checkPort = New ScanPort(IPaddress, Port) 'Creates a new instance of the ScanPort class, starting the desired port scan.
- Next
- Catch ex As Exception
- Working = False
- End Try
- Working = False
- End Sub
- Private Sub ScanPort_Result(ByVal Port As String, ByVal Success As Boolean) Handles checkPort.Result
- If Success Then 'If the connection was successful, then it will be added to be seen.
- newPort = Port 'Sets the local value newPort to the current port.
- addItem() 'Adds the item to the listbox.
- End If
- End Sub
- Private Sub addItem()
- If Me.lbPorts.InvokeRequired Then 'If this sub is runned in another thread, invoke the UI thread to edit it's objects.
- Me.lbPorts.Invoke(New MethodInvoker(AddressOf addItem)) 'Code not being runned by the main thread, therefore is invoking the object to edit it.
- Else
- Me.lbPorts.Items.Add("Open port: " & newPort) 'Adds the open port to the listbox.
- End If
- End Sub
- End Class
- Public Class ScanPort 'A class for to scan ports
- Public Shared Event Result(ByVal Port As String, ByVal Success As Boolean)
- Private IP As String, Port As Integer
- Sub New(ByVal IP As String, ByVal Port As Integer)
- Me.IP = IP 'Sets the local value 'IP' of the class to the new desired IP.
- Me.Port = Port 'Sets the local value 'Port' of the class to the new desired Port.
- Dim tmpThread As New Thread(AddressOf checkPort) 'Creates a new temporary thread to run the port scan on.
- tmpThread.Start() 'Starts the new thread.
- End Sub
- Sub checkPort()
- Dim tmpClient As New TcpClient() 'Creates a new instance of the temporary TCPclient.
- Try
- tmpClient.Connect(IP, Port) 'Connects to the selected port by the desired IP.
- Thread.Sleep(50) 'This is the value in miliseconds the TCPclient have to connect, before timing out.
- RaiseEvent Result(Port, tmpClient.Connected) 'Raising the event 'Result' with the desired answer, wheter it succeeded or failed.
- tmpClient.Close()
- Catch ex As Exception
- tmpClient.Close()
- RaiseEvent Result(Port, False) 'Some error happened while trying to connect, return the value of false.
- End Try
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement