My first non introductory post, I fort i will start the coding with a small port scanner, This is made in C# using the .Net 4 framework. It scans from port Zero to Sixty-Five Thousand Five Hundred and Thirty Six as default. Once the scanner has started scanning each port, It will display the outcome as IP Address, Port closed or open, for example "192.168.0.5's Port 2 is open" if the port is open or "192.168.0.5's Port 2 is closed" if the port is closed. Anyways there is a small diagram showing the Graphical User Interface (GUI) and the code written underneath it which is well commented so you shouldn't have any problems.
The Tools I used and the names of the Tools are as followed;
private System.Windows.Forms.Button btnScan;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.GroupBox grpControlls;
private System.Windows.Forms.ProgressBar progBar;
private System.Windows.Forms.Label lblIPAddress;
private System.Windows.Forms.TextBox txtIPAddress;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.ComboBox cmbPortStart;
private System.Windows.Forms.ComboBox cmbPortEnd;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.GroupBox grpControlls;
private System.Windows.Forms.ProgressBar progBar;
private System.Windows.Forms.Label lblIPAddress;
private System.Windows.Forms.TextBox txtIPAddress;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.ComboBox cmbPortStart;
private System.Windows.Forms.ComboBox cmbPortEnd;
The Code is as followed;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Port_Scanner
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnScan_Click(object sender, EventArgs e)
{
// Store the values of port numbers.
int beginPort;
int endPort;
//If theres an ipaddress continue else do nothing.
if (txtIPAddress.Text != null)
{
//If theres a port number continue else do nothing.
if (cmbPortStart.Text != null | cmbPortEnd.Text != null)
{
//Converts the port number from a string to an int.
beginPort = Convert.ToInt32(cmbPortStart.Text);
endPort = Convert.ToInt32(cmbPortEnd.Text);
//If the first port is smaller than the second port continue.
if (beginPort < endPort)
{
//Set the minimum value of the progress bar.
progBar.Minimum = beginPort;
// Set the max value of the progress bar.
progBar.Maximum = endPort;
// Resets the progress bar to smallest port number.
progBar.Value = beginPort;
//Increases the progress bar by one.
progBar.Step = 1;
// Let the user know the application is in prgress.
Cursor.Current = Cursors.WaitCursor;
// Loop through the ports between start port and end port.
for (int CurrPort = beginPort; CurrPort <= endPort; CurrPort++)
{
System.Net.Sockets.TcpClient TcpScanPort = new System.Net.Sockets.TcpClient();
try
{
// Try to connect
TcpScanPort.Connect(txtIPAddress.Text, CurrPort);
// If there's no exception, we can say the port is open
txtResult.AppendText(txtIPAddress.Text + "'s Port " + CurrPort + " is open" + Environment.NewLine);
//Closes the port.
TcpScanPort.Close();
}
catch
{
// An exception occured, thus the port is probably closed.
txtResult.AppendText(txtIPAddress.Text + "'s Port " + CurrPort + " is closed" + Environment.NewLine);
}
// Increase the progress on the progress bar
progBar.PerformStep();
}
// Set the cursor back to normal
Cursor.Current = Cursors.Arrow;
}
else
{
//Error has accoured, do nothing.
}
}
else
{
//Error has accoured, do nothing.
}
}
else
{
//Error has accoured, do nothing.
}
}
private void frmMain_Load(object sender, EventArgs e)
{
//Adds numbers from zero to sixty-five thousand five hundred and thirty six to the two ComboBoxes.
for (int i = 0; i < 65536; i++)
{
cmbPortStart.Items.Add(i);
cmbPortEnd.Items.Add(i);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Port_Scanner
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnScan_Click(object sender, EventArgs e)
{
// Store the values of port numbers.
int beginPort;
int endPort;
//If theres an ipaddress continue else do nothing.
if (txtIPAddress.Text != null)
{
//If theres a port number continue else do nothing.
if (cmbPortStart.Text != null | cmbPortEnd.Text != null)
{
//Converts the port number from a string to an int.
beginPort = Convert.ToInt32(cmbPortStart.Text);
endPort = Convert.ToInt32(cmbPortEnd.Text);
//If the first port is smaller than the second port continue.
if (beginPort < endPort)
{
//Set the minimum value of the progress bar.
progBar.Minimum = beginPort;
// Set the max value of the progress bar.
progBar.Maximum = endPort;
// Resets the progress bar to smallest port number.
progBar.Value = beginPort;
//Increases the progress bar by one.
progBar.Step = 1;
// Let the user know the application is in prgress.
Cursor.Current = Cursors.WaitCursor;
// Loop through the ports between start port and end port.
for (int CurrPort = beginPort; CurrPort <= endPort; CurrPort++)
{
System.Net.Sockets.TcpClient TcpScanPort = new System.Net.Sockets.TcpClient();
try
{
// Try to connect
TcpScanPort.Connect(txtIPAddress.Text, CurrPort);
// If there's no exception, we can say the port is open
txtResult.AppendText(txtIPAddress.Text + "'s Port " + CurrPort + " is open" + Environment.NewLine);
//Closes the port.
TcpScanPort.Close();
}
catch
{
// An exception occured, thus the port is probably closed.
txtResult.AppendText(txtIPAddress.Text + "'s Port " + CurrPort + " is closed" + Environment.NewLine);
}
// Increase the progress on the progress bar
progBar.PerformStep();
}
// Set the cursor back to normal
Cursor.Current = Cursors.Arrow;
}
else
{
//Error has accoured, do nothing.
}
}
else
{
//Error has accoured, do nothing.
}
}
else
{
//Error has accoured, do nothing.
}
}
private void frmMain_Load(object sender, EventArgs e)
{
//Adds numbers from zero to sixty-five thousand five hundred and thirty six to the two ComboBoxes.
for (int i = 0; i < 65536; i++)
{
cmbPortStart.Items.Add(i);
cmbPortEnd.Items.Add(i);
}
}
}
}
No comments:
Post a Comment