Creating a Port Scanner with C#

Learn how to create a simple port scanning tool in .NET, that allows you to scan IPs for open ports. It uses sockets that try to connect to the ports.

An IP scanning tool can prove useful when you want to check the security of your computer. Having an open port doesn’t necessarily mean an invitation to hackers because several applications and services use ports to communicate through the Internet. For example it’s normal for a web server to have port 80 open, or for an FTP server to have port 21 open.

The way we’re going to check for open parts might not be the fastest or most precise method, but it’s the simplest. We’re going to use a TcpClient to try and connect to each port. If the code raises an exception, it’s probably because the connection was not successful. However, if there’s no exception it means the TcpClient successfully connected to the port and thus the port is open.

First we need to use this using directive so we don’t have to write really long lines of code:

using System.Net.Sockets;

Before implemeting a nice Windows Form where the user can input the IP and the port range to be scanned, let’s hardcode the values to see how the scanning is actually done. Suppose we want to scan the ports from 1 to 25 for the IP 192.168.0.1 (a local network IP):

for (int CurrPort = 1; CurrPort <= 25; CurrPort++)

{

TcpClient TcpScan = new TcpClient();

try


{

// Try to connect

TcpScan.Connect("192.168.0.1", CurrPort);

// If there's no exception, we can say the port is open

MessageBox.Show("Port " + CurrPort + " open");

}

catch

{

// An exception occured, thus the port is probably closed

MessageBox.Show("Port " + CurrPort + " closed");

}

}

We loop from 1 to 25 (the ports) and try to connect, just like I explained earlier. That’s all the code we need.

The Visual Studio 2005 project attached to this tutorial is a simple Windows Forms which you can see in the screenshot below:

The code for this application can be found in the click event of the Start Scan button (btnStart):

// Store values from the NumericUpDown to variables

StartPort = Convert.ToInt32(numStart.Value);

EndPort = Convert.ToInt32(numEnd.Value);

// Reset the progress bar

prgScanning.Value = 0;

// Set the max value of the progress bar

prgScanning.Maximum = EndPort - StartPort + 1;

// Let the user know the application is busy

Cursor.Current = Cursors.WaitCursor;

// Loop through the ports between start port and end port

for (int CurrPort = StartPort; CurrPort <= EndPort; CurrPort++)

{

TcpClient TcpScan = new TcpClient();

try


{

// Try to connect

TcpScan.Connect(txtIP.Text, CurrPort);

// If there's no exception, we can say the port is open

txtLog.AppendText("Port " + CurrPort + " open");

}

catch

{

// An exception occured, thus the port is probably closed

txtLog.AppendText("Port " + CurrPort + " closed");

}

// Increase the progress on the progress bar

prgScanning.PerformStep();

}

// Set the cursor back to normal

Cursor.Current = Cursors.Arrow;
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top