Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Visual Basic

VB Port Scanner

Rate me:
Please Sign up or sign in to vote.
2.95/5 (10 votes)
20 Jan 2007CPOL 97.5K   5.4K   45   9
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)


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

Comments and Discussions

 
GeneralMy vote of 1 Pin
Ariel2k29-Jan-09 22:48
Ariel2k29-Jan-09 22:48 
GeneralChecks only in use ports !!! Pin
Tauhid Shaikh27-Sep-07 4:26
Tauhid Shaikh27-Sep-07 4:26 
GeneralApplication Bug !!! Pin
Tauhid Shaikh26-Sep-07 1:55
Tauhid Shaikh26-Sep-07 1:55 
GeneralWhere is multithreading... Pin
hakervytas20-Jan-07 22:23
hakervytas20-Jan-07 22:23 
GeneralRe: Where is multithreading... Pin
Dave@VB21-Jan-07 2:56
Dave@VB21-Jan-07 2:56 
GeneralRe: Where is multithreading... Pin
Dave@VB21-Jan-07 3:42
Dave@VB21-Jan-07 3:42 
GeneralRe: Where is multithreading... Pin
Ray Cassick21-Jan-07 6:39
Ray Cassick21-Jan-07 6:39 
GeneralRe: Where is multithreading... Pin
Syed M Hussain21-Jan-07 6:49
Syed M Hussain21-Jan-07 6:49 
GeneralRe: Where is multithreading... Pin
Dr.Serdar26-Apr-07 22:01
Dr.Serdar26-Apr-07 22:01 

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.