欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于C#实现的端口扫描器实例代码

程序员文章站 2024-02-20 22:55:34
本文所述为基于c#实现的端口扫描器代码,代码内包括了窗体和逻辑处理两部分代码。在代码中,创建tcpclient对象,把tcpclient用于为tcp网络服务提供客户端连接,...

本文所述为基于c#实现的端口扫描器代码,代码内包括了窗体和逻辑处理两部分代码。在代码中,创建tcpclient对象,把tcpclient用于为tcp网络服务提供客户端连接,创建线程,并创建threadstart委托对象,端口扫描范围为[0-65536],也可自定义扫描范围。

功能主要代码如下:

using system; 
using system.drawing; 
using system.windows.forms; 
using system.net.sockets; 
using system.threading; 
namespace portscanner 
{ 
 public class form1: system.windows.forms.form 
 { 
 private system.windows.forms.textbox txtaddr; 
 private system.windows.forms.textbox txtstart; 
 private system.windows.forms.textbox txtend; 
 private system.windows.forms.label label1; 
 private system.windows.forms.label label2; 
 private system.windows.forms.label label3; 
 private system.windows.forms.splitter splitter1; 
 private system.windows.forms.listbox lbresult; 
 private system.windows.forms.button btnscan; 
 private system.windows.forms.progressbar progressbar1; 
 private system.windows.forms.label lblstart; 
 private system.windows.forms.label lblstop; 
 private system.windows.forms.label lblnow; 
 //自定义变量
 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() 
 { 
  this.lbresult = new system.windows.forms.listbox();
  this.txtaddr = new system.windows.forms.textbox();
  this.txtstart = new system.windows.forms.textbox();
  this.txtend = new system.windows.forms.textbox();
  this.label1 = new system.windows.forms.label();
  this.label2 = new system.windows.forms.label();
  this.label3 = new system.windows.forms.label();
  this.splitter1 = new system.windows.forms.splitter();
  this.btnscan = new system.windows.forms.button();
  this.progressbar1 = new system.windows.forms.progressbar();
  this.lblstart = new system.windows.forms.label();
  this.lblstop = new system.windows.forms.label();
  this.lblnow = new system.windows.forms.label();
  this.suspendlayout();
  // 
  // lbresult
  // 
  this.lbresult.dock = system.windows.forms.dockstyle.right;
  this.lbresult.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.lbresult.itemheight = 14;
  this.lbresult.location = new system.drawing.point(224, 0);
  this.lbresult.name = "lbresult";
  this.lbresult.size = new system.drawing.size(264, 270);
  this.lbresult.tabindex = 0;
  // 
  // txtaddr
  // 
  this.txtaddr.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.txtaddr.location = new system.drawing.point(72, 16);
  this.txtaddr.name = "txtaddr";
  this.txtaddr.size = new system.drawing.size(136, 22);
  this.txtaddr.tabindex = 1;
  this.txtaddr.text = "";
  // 
  // txtstart
  // 
  this.txtstart.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.txtstart.location = new system.drawing.point(72, 48);
  this.txtstart.name = "txtstart";
  this.txtstart.size = new system.drawing.size(136, 22);
  this.txtstart.tabindex = 2;
  this.txtstart.text = "";
  this.txtstart.textchanged += new system.eventhandler(this.txtstart_textchanged);
  // 
  // txtend
  // 
  this.txtend.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.txtend.location = new system.drawing.point(72, 80);
  this.txtend.name = "txtend";
  this.txtend.size = new system.drawing.size(136, 22);
  this.txtend.tabindex = 3;
  this.txtend.text = "";
  this.txtend.textchanged += new system.eventhandler(this.txtend_textchanged);
  // 
  // label1
  // 
  this.label1.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.label1.location = new system.drawing.point(8, 16);
  this.label1.name = "label1";
  this.label1.size = new system.drawing.size(64, 23);
  this.label1.tabindex = 4;
  this.label1.text = "主机地址";
  // 
  // label2
  // 
  this.label2.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.label2.location = new system.drawing.point(8, 80);
  this.label2.name = "label2";
  this.label2.size = new system.drawing.size(64, 23);
  this.label2.tabindex = 5;
  this.label2.text = "结束端口";
  // 
  // label3
  // 
  this.label3.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.label3.location = new system.drawing.point(8, 48);
  this.label3.name = "label3";
  this.label3.size = new system.drawing.size(64, 23);
  this.label3.tabindex = 6;
  this.label3.text = "起始端口";
  // 
  // splitter1
  // 
  this.splitter1.dock = system.windows.forms.dockstyle.right;
  this.splitter1.location = new system.drawing.point(221, 0);
  this.splitter1.name = "splitter1";
  this.splitter1.righttoleft = system.windows.forms.righttoleft.yes;
  this.splitter1.size = new system.drawing.size(3, 273);
  this.splitter1.tabindex = 7;
  this.splitter1.tabstop = false;
  // 
  // btnscan
  // 
  this.btnscan.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.btnscan.location = new system.drawing.point(72, 200);
  this.btnscan.name = "btnscan";
  this.btnscan.tabindex = 8;
  this.btnscan.text = "扫描";
  this.btnscan.click += new system.eventhandler(this.btnscan_click);
  // 
  // progressbar1
  // 
  this.progressbar1.location = new system.drawing.point(8, 112);
  this.progressbar1.name = "progressbar1";
  this.progressbar1.size = new system.drawing.size(200, 23);
  this.progressbar1.step = 1;
  this.progressbar1.tabindex = 9;
  // 
  // lblstart
  // 
  this.lblstart.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.lblstart.location = new system.drawing.point(8, 144);
  this.lblstart.name = "lblstart";
  this.lblstart.size = new system.drawing.size(48, 23);
  this.lblstart.tabindex = 10;
  // 
  // lblstop
  // 
  this.lblstop.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.lblstop.location = new system.drawing.point(160, 144);
  this.lblstop.name = "lblstop";
  this.lblstop.size = new system.drawing.size(48, 23);
  this.lblstop.tabindex = 11;
  // 
  // lblnow
  // 
  this.lblnow.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.lblnow.location = new system.drawing.point(84, 144);
  this.lblnow.name = "lblnow";
  this.lblnow.size = new system.drawing.size(48, 23);
  this.lblnow.tabindex = 12;
  this.autoscalebasesize = new system.drawing.size(6, 15);
  this.clientsize = new system.drawing.size(488, 273);
  this.controls.add(this.lblnow);
  this.controls.add(this.lblstop);
  this.controls.add(this.lblstart);
  this.controls.add(this.progressbar1);
  this.controls.add(this.btnscan);
  this.controls.add(this.splitter1);
  this.controls.add(this.label3);
  this.controls.add(this.label2);
  this.controls.add(this.label1);
  this.controls.add(this.txtend);
  this.controls.add(this.txtstart);
  this.controls.add(this.txtaddr);
  this.controls.add(this.lbresult);
  this.font = new system.drawing.font("tahoma", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(0)));
  this.name = "form1";
  this.text = "端口扫描器";
  this.resumelayout(false);
 } 
 [stathread] 
 static void main() 
 { 
  application.run(new form1()); 
 } 
 private void btnscan_click(object sender, system.eventargs e) 
 { 
  //创建线程,并创建threadstart委托对象
  thread process = new thread(new threadstart(portscan)); 
  process.start(); 
  //显示端口扫描的范围
  progressbar1.minimum = int32.parse(txtstart.text); 
  progressbar1.maximum = int32.parse(txtend.text); 
  //显示框初始化
  lbresult.items.clear(); 
  lbresult.items.add("端口扫描器 v1.0."); 
  lbresult.items.add(""); 
 } 
 private void portscan() 
 { 
  start = int32.parse(txtstart.text); 
  end = int32.parse(txtend.text); 
  //检查输入范围合法性
  if ((start >= 0 && start <= 65536) && (end >= 0 && end <= 65536) && (start <= end)) 
  { 
  lbresult.items.add("开始扫描... (可能需要请您等待几分钟)"); 
  addr = txtaddr.text; 
  for (int i = start; i <= end; i++) 
  { 
   port = i; 
   //使用该端口的扫描线程
   scanthread = new thread(new threadstart(scan)); 
   scanthread.start(); 
   //使线程睡眠
   system.threading.thread.sleep(100); 
   progressbar1.value = i; 
   lblnow.text = i.tostring(); 
  } 
  //未完成时情况
  while (!ok) 
  { 
   ok = true; 
   for (int i = start; i <= end; i++) 
   { 
   if (!done[i]) 
   { 
    ok = false; 
    break; 
   } 
   } 
   system.threading.thread.sleep(1000); 
  } 
  lbresult.items.add("扫描结束!"); 
  } 
  else 
  { 
  messagebox.show("输入错误,端口范围为[0-65536]"); 
  } 
 } 
 private void scan() 
 { 
  int portnow = port; 
  //创建线程变量
  thread threadnow = scanthread; 
  done[portnow] = true; 
  //创建tcpclient对象,tcpclient用于为tcp网络服务提供客户端连接
  tcpclient objtcp = null; 
  //扫描端口,成功则写入信息
  try 
  { 
  //用tcpclient对象扫描端口
  objtcp = new tcpclient(addr, portnow); 
  lbresult.items.add("端口 " + portnow.tostring() + " 开放!"); 
  } 
  catch 
  {
  } 
 } 
 private void txtstart_textchanged(object sender, system.eventargs e) 
 { 
  //获取输入的起始端口值
  lblstart.text = txtstart.text; 
 } 
 private void txtend_textchanged(object sender, system.eventargs e) 
 { 
  //获取输入的接受端口值
  lblstop.text = txtend.text; 
 } 
 } 
}