Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

VB Port Scanner

2.95/5 (10 votes)
20 Jan 2007CPOL 1   5.5K  
Scan for open ports on a host.

Sample Image

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

VB
'First declare or variables
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.

VB
Private Sub Timer1_Tick(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Timer1.Tick
    'Set the host and port and counter
    counter = counter + 1 'counter is for the timer
    TextBox2.Text = counter
    host = TextBox1.Text
    port = TextBox2.Text
    ' Next part creates a socket to try and connect 
    ' on with the given user information.
 
    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.

VB
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:

VB
Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
    Button2.Enabled = False
    TextBox2.Text = "0"
    'set counter explained before to 0
    counter = 0
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button2.Click
    'stop button
    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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)