SoFunction
Updated on 2025-03-06

Port scanner instance code based on C# implementation

This article describes the port scanner code based on C# implementation, and the code includes two parts of code: form and logic processing. In the code, create a TcpClient object, use TcpClient to provide client connections for TCP network services, create threads, and create a ThreadStart delegate object. The port scan range is [0-65536], and the scan range can also be customized.

The main function code is as follows:

using System; 
using ; 
using ; 
using ; 
using ; 
namespace PortScanner 
{ 
 public class Form1:  
 { 
 private  txtAddr; 
 private  txtStart; 
 private  txtEnd; 
 private  label1; 
 private  label2; 
 private  label3; 
 private  splitter1; 
 private  lbResult; 
 private  btnScan; 
 private  progressBar1; 
 private  lblStart; 
 private  lblStop; 
 private  lblNow; 
 //Custom variables private int port; 
 private string Addr; 
 private bool[] done = new bool[65536]; 
 private int start; 
 private int end; 
 private Thread scanThread; 
 private bool OK; 
 public Form1() 
 { 
  InitializeComponent(); 
 } 
 private void InitializeComponent() 
 { 
   = new ();
   = new ();
   = new ();
   = new ();
  this.label1 = new ();
  this.label2 = new ();
  this.label3 = new ();
  this.splitter1 = new ();
   = new ();
  this.progressBar1 = new ();
   = new ();
   = new ();
   = new ();
  ();
  // 
  // lbResult
  // 
   = ;
   = new ("Tahoma", 9F, , , (()(0)));
   = 14;
   = new (224, 0);
   = "lbResult";
   = new (264, 270);
   = 0;
  // 
  // txtAddr
  // 
   = new ("Tahoma", 9F, , , (()(0)));
   = new (72, 16);
   = "txtAddr";
   = new (136, 22);
   = 1;
   = "";
  // 
  // txtStart
  // 
   = new ("Tahoma", 9F, , , (()(0)));
   = new (72, 48);
   = "txtStart";
   = new (136, 22);
   = 2;
   = "";
   += new (this.txtStart_TextChanged);
  // 
  // txtEnd
  // 
   = new ("Tahoma", 9F, , , (()(0)));
   = new (72, 80);
   = "txtEnd";
   = new (136, 22);
   = 3;
   = "";
   += new (this.txtEnd_TextChanged);
  // 
  // label1
  // 
  this. = new ("Tahoma", 9F, , , (()(0)));
  this. = new (8, 16);
  this. = "label1";
  this. = new (64, 23);
  this. = 4;
  this. = "Host Address";
  // 
  // label2
  // 
  this. = new ("Tahoma", 9F, , , (()(0)));
  this. = new (8, 80);
  this. = "label2";
  this. = new (64, 23);
  this. = 5;
  this. = "End Port";
  // 
  // label3
  // 
  this. = new ("Tahoma", 9F, , , (()(0)));
  this. = new (8, 48);
  this. = "label3";
  this. = new (64, 23);
  this. = 6;
  this. = "Start Port";
  // 
  // splitter1
  // 
  this. = ;
  this. = new (221, 0);
  this. = "splitter1";
  this. = ;
  this. = new (3, 273);
  this. = 7;
  this. = false;
  // 
  // btnScan
  // 
   = new ("Tahoma", 9F, , , (()(0)));
   = new (72, 200);
   = "btnScan";
   = 8;
   = "scanning";
   += new (this.btnScan_Click);
  // 
  // progressBar1
  // 
  this. = new (8, 112);
  this. = "progressBar1";
  this. = new (200, 23);
  this. = 1;
  this. = 9;
  // 
  // lblStart
  // 
   = new ("Tahoma", 9F, , , (()(0)));
   = new (8, 144);
   = "lblStart";
   = new (48, 23);
   = 10;
  // 
  // lblStop
  // 
   = new ("Tahoma", 9F, , , (()(0)));
   = new (160, 144);
   = "lblStop";
   = new (48, 23);
   = 11;
  // 
  // lblNow
  // 
   = new ("Tahoma", 9F, , , (()(0)));
   = new (84, 144);
   = "lblNow";
   = new (48, 23);
   = 12;
   = new (6, 15);
   = new (488, 273);
  ();
  ();
  ();
  (this.progressBar1);
  ();
  (this.splitter1);
  (this.label3);
  (this.label2);
  (this.label1);
  ();
  ();
  ();
  ();
   = new ("Tahoma", 9F, , , (()(0)));
   = "Form1";
   = "Port Scanner";
  (false);
 } 
 [STAThread] 
 static void Main() 
 { 
  (new Form1()); 
 } 
 private void btnScan_Click(object sender,  e) 
 { 
  //Create a thread and create a ThreadStart delegate object  Thread process = new Thread(new ThreadStart(PortScan)); 
  (); 
  //Show the range of port scan   = (); 
   = (); 
  //Initialization of display box  (); 
  ("Port Scanner v1.0."); 
  (""); 
 } 
 private void PortScan() 
 { 
  start = (); 
  end = (); 
  //Check the legality of the input range  if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end)) 
  { 
  ("Start scanning... (You may need to wait a few minutes)"); 
  Addr = ; 
  for (int i = start; i <= end; i++) 
  { 
   port = i; 
   //Scan thread using this port   scanThread = new Thread(new ThreadStart(Scan)); 
   (); 
   //Make the thread sleep   (100); 
    = i; 
    = (); 
  } 
  //Incomplete situation  while (!OK) 
  { 
   OK = true; 
   for (int i = start; i <= end; i++) 
   { 
   if (!done[i]) 
   { 
    OK = false; 
    break; 
   } 
   } 
   (1000); 
  } 
  ("Scan ends!"); 
  } 
  else 
  { 
  ("Input error, port range is [0-65536]"); 
  } 
 } 
 private void Scan() 
 { 
  int portnow = port; 
  //Create thread variable  Thread Threadnow = scanThread; 
  done[portnow] = true; 
  //Create TcpClient object, TcpClient is used to provide client connections to TCP network services  TcpClient objTCP = null; 
  //Scan the port and write information if it is successful  try 
  { 
  //Scan the port with the TcpClient object  objTCP = new TcpClient(Addr, portnow); 
  ("Port" + () + "Open!"); 
  } 
  catch 
  {
  } 
 } 
 private void txtStart_TextChanged(object sender,  e) 
 { 
  //Get the input start port value   = ; 
 } 
 private void txtEnd_TextChanged(object sender,  e) 
 { 
  //Get the input accept port value   = ; 
 } 
 } 
}