How to realize Port Scanner based on C#
This article is about how to implement port scanner based on C #. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Create a new project and set up the interface
Create a new project:
Select the Windows forms project application (.Net Framework):
Set the project name and path:
The new projects are as follows:
Setup interface:
Set tbShow to read-only:
Second, single-thread port scanning 1. Write code
Double-click the button to write its click event:
Using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace Scan {public partial class Form1: Form {/ / host address private string hostAddress; / / start port private int start; / / end port private int end / / Port number private int port; / / define thread object private Thread scanThread; public Form1 () {InitializeComponent () } private void button1_Click_1 (object sender, EventArgs e) {try {/ / initialize tbShow.Clear (); lb.Text = "0%"; / / get ip address and start and end number hostAddress = tbHost.Text Start = Int32.Parse (tbSPort.Text); end = Int32.Parse (tbEPort.Text); if (decideAddress ()) / / the port is reasonable {/ / make the input textbox read-only and cannot change tbHost.ReadOnly = true; tbSPort.ReadOnly = true TbEPort.ReadOnly = true; / / set the range of the progress bar pb.Minimum = start; pb.Maximum = end; / / the display box shows tbShow.AppendText ("Port Scanner v1.0.0" + Environment.NewLine + Environment.NewLine) / / call the port scan function PortScan ();} else {/ / if the port number is unreasonable, the pop-up window reports an error MessageBox.Show ("input error, port range is [0-65536]!") }} catch {/ / if the port number entered is non-integer, the pop-up window reports an error MessageBox.Show ("input error, port range is [0-65536]!") }} / determine whether the port is reasonable / private bool decideAddress () {/ / determine whether the port number is reasonable if ((start > = 0 & & start = 0 & & end)