Advertisement
Frekvens1

[Visual Basic] TCP Port scanner by Frekvens1

Jul 7th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'CODE WRITTEN BY FREKVENS1, www.Frekvens1.webs.com.
  2. '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!
  3. 'https://www.youtube.com/watch?v=5qpn0vxKr6c'
  4. 'Feel free to use the code at own will, and if you need help understanding it better give me a call!
  5. 'This is not the optimal code, but instead used to learn more.
  6. 'Thanks for watching my code, and I hope you find something useful in here! (VB.NET).
  7. Imports System.Threading
  8. Imports System.Net.Sockets
  9.  
  10. Public Class Form1
  11.     WithEvents checkPort As ScanPort
  12.     Private newPort As String, Working As Boolean
  13.  
  14.     Private Sub btnScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScan.Click
  15.         If Working Then
  16.             MsgBox("[ERROR] Scan is still running!", MsgBoxStyle.Critical)
  17.         Else
  18.             Try
  19.                 lbPorts.Items.Clear() 'Clears the listbox for a new scan.
  20.                Working = True
  21.                 Dim t As New Thread(AddressOf startScan) 'Lag preventer.
  22.                t.Start()
  23.             Catch ex As Exception
  24.                 Working = False
  25.             End Try
  26.         End If
  27.        
  28.     End Sub
  29.  
  30.     Private Sub startScan() 'Just to make the UI (User Interface (Main Thread) ) less 'laggy'.
  31.        checkPorts(txtIP.Text, nudStart.Value, nudStop.Value) 'Starts the port scanner, checking each of the desired ports.
  32.    End Sub
  33.  
  34.     Private Sub checkPorts(ByVal IPaddress As String, ByVal PortRangeStart As Integer, ByVal PortRangeMax As Integer)
  35.         Working = True
  36.  
  37.         Try
  38.             For Port = PortRangeStart To PortRangeMax 'Increases the port value by 1 until it reaches the max value, starting from the start value.
  39.                Thread.Sleep(20) 'Not needed, but keeps the software from freezing.
  40.                checkPort = New ScanPort(IPaddress, Port) 'Creates a new instance of the ScanPort class, starting the desired port scan.
  41.            Next
  42.         Catch ex As Exception
  43.             Working = False
  44.         End Try
  45.  
  46.         Working = False
  47.     End Sub
  48.  
  49.     Private Sub ScanPort_Result(ByVal Port As String, ByVal Success As Boolean) Handles checkPort.Result
  50.         If Success Then 'If the connection was successful, then it will be added to be seen.
  51.            newPort = Port 'Sets the local value newPort to the current port.
  52.            addItem() 'Adds the item to the listbox.
  53.        End If
  54.     End Sub
  55.  
  56.     Private Sub addItem()
  57.         If Me.lbPorts.InvokeRequired Then 'If this sub is runned in another thread, invoke the UI thread to edit it's objects.
  58.            Me.lbPorts.Invoke(New MethodInvoker(AddressOf addItem)) 'Code not being runned by the main thread, therefore is invoking the object to edit it.
  59.        Else
  60.             Me.lbPorts.Items.Add("Open port: " & newPort) 'Adds the open port to the listbox.
  61.        End If
  62.     End Sub
  63. End Class
  64.  
  65. Public Class ScanPort 'A class for to scan ports
  66.    Public Shared Event Result(ByVal Port As String, ByVal Success As Boolean)
  67.     Private IP As String, Port As Integer
  68.  
  69.     Sub New(ByVal IP As String, ByVal Port As Integer)
  70.         Me.IP = IP 'Sets the local value 'IP' of the class to the new desired IP.
  71.        Me.Port = Port 'Sets the local value 'Port' of the class to the new desired Port.
  72.  
  73.         Dim tmpThread As New Thread(AddressOf checkPort) 'Creates a new temporary thread to run the port scan on.
  74.        tmpThread.Start() 'Starts the new thread.
  75.    End Sub
  76.  
  77.     Sub checkPort()
  78.         Dim tmpClient As New TcpClient() 'Creates a new instance of the temporary TCPclient.
  79.        Try
  80.             tmpClient.Connect(IP, Port) 'Connects to the selected port by the desired IP.
  81.            Thread.Sleep(50) 'This is the value in miliseconds the TCPclient have to connect, before timing out.
  82.            RaiseEvent Result(Port, tmpClient.Connected) 'Raising the event 'Result' with the desired answer, wheter it succeeded or failed.
  83.            tmpClient.Close()
  84.         Catch ex As Exception
  85.             tmpClient.Close()
  86.             RaiseEvent Result(Port, False) 'Some error happened while trying to connect, return the value of false.
  87.        End Try
  88.     End Sub
  89. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement