Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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

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

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:

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)

Share

About the Author

Dave@VB

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

Comments and Discussions

 
GeneralMy vote of 1 PinmemberAriel2k29-Jan-09 22:48 
GeneralChecks only in use ports !!! PinmemberTauhid Shaikh27-Sep-07 4:26 
GeneralApplication Bug !!! PinmemberTauhid Shaikh26-Sep-07 1:55 
GeneralWhere is multithreading... Pinmemberhakervytas20-Jan-07 22:23 
GeneralRe: Where is multithreading... PinmemberDave@VB21-Jan-07 2:56 
GeneralRe: Where is multithreading... PinmemberDave@VB21-Jan-07 3:42 
GeneralRe: Where is multithreading... PinmemberRay Cassick21-Jan-07 6:39 
GeneralRe: Where is multithreading... PinmemberSyed M Hussain21-Jan-07 6:49 
GeneralRe: Where is multithreading... PinmemberDr.Serdar26-Apr-07 22:01 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Jan 2007
Article Copyright 2007 by Dave@VB
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid