Click here to Skip to main content
13,354,132 members (56,294 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 20 Jan 2007

VB Port Scanner

, 20 Jan 2007
Rate this:
Please Sign up or sign in to vote.
Scan for open ports on a host.

Sample Image


This program scans for open ports. You supply a host, e.g.:, hit Start, and the program scans.

Using the Code

'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.

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 = _
    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, _
    End Try
    If Not s.Connected Then
        ListBox1.Items.Add("Port " + port.ToString + " is not open")
        ListBox1.Items.Add("Port " + port.ToString + " is open")
    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)
    Button2.Enabled = True
    Button1.Enabled = False
    Timer1.Enabled = True
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"
    '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.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.


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


About the Author

United States United States
Learning Visual Basic as I go. Work with Visual Basic 2005 Express Edition.

You may also be interested in...

Comments and Discussions

GeneralApplication Bug !!! Pin
Tauhid Shaikh26-Sep-07 2:55
memberTauhid Shaikh26-Sep-07 2:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 20 Jan 2007
Article Copyright 2007 by Dave@VB
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid