当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-10-31

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

本文所述为基于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 = 0 && end 

    
 
 

您可能感兴趣的文章:

  • c#通过委托delegate与Dictionary实现action选择器代码举例
  • C#实现获取枚举中元素个数的方法
  • C#实现自定义双击事件
  • C#键盘输入回车键实现点击按钮效果的方法
  • C#实现获取一年中是第几个星期的方法
  • C#实现Datatable排序的方法
  • C#实现装箱与拆箱操作简单实例
  • 解决C#中WebBrowser的DocumentCompleted事件不执行的实现方法
  • C#下实现创建和删除目录的实例代码
  • sqlserver iis7站长之家
  • C#实现过滤html标签并保留a标签的方法
  • c#实现TextBox只允许输入数字
  • C# Winform 整个窗口拖动的实现代码
  • c# ListView实现双击Item事件的变通方法
  • C#实现随鼠标移动窗体实例
  • C#中的FileUpload 选择后的预览效果具体实现
  • C# 窗体隐藏及任务管理器中禁止关闭的实现代码
  • C#的锯齿数组以及C++实现代码
  • C#格式化文件大小的实现代码
  • C#怎样才能实现窗体最小化到托盘呢?
  • C# char类型字符转换大小写的实现代码
  • C实现的非阻塞方式命令行端口扫描器源码
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Linux下的Socket通信如何断开连接的端口从而实现重复使用该端口
  • windows 下的PortTunnel 在linux下怎么实现?或者相应的已经实现的软件?端口映射
  • 求教用一个端口能否实现3G视频传输
  • 在linux下能实现多端口映射吗?
  • 怎样编程实现I/O端口的读写?
  • 怎样在TURBO LINUX下实现静态端口映射
  • bind9架设内部dns+硬件防火墙端口映射,不能实现域名解析
  • 在一个socket守护服务进程中,如何实现同时监听两个端口?
  • python判断端口是否打开的实现代码
  • 一般的tcpip实现都会提供ftp,telnet,daytime,以及time等基本服务,linux上提供13端口的daytime服务吗?
  • 我想在服务器端口获得连结上来机子的IP,不知道有什么方法可以实现!!!?
  • 使用python实现扫描端口示例
  • 求助!如何使用同一个套接字和端口实现反复下载
  • 关于使用iptables实现udp端口的nat的问题
  • shell脚本结合iptables防端口扫描的实现
  • Python实现根据指定端口探测服务器/模块部署的方法
  • python实现数通设备端口监控示例
  • 非阻塞connect实现端口扫描的问题-connect提前返回怎么处理?
  • Python实现端口复用实例代码
  • 通过javascript实现DIV居中,兼容各浏览器版本
  • socket实现多文件并发传输,求助多线程实现问题?
  • Python GUI编程:tkinter实现一个窗口并居中代码
  • interface 到底有什么用???实现接口,怎么实现??
  • 通过javascript库JQuery实现页面跳转功能代码
  • 怎么用Jsp实现在页面实现树型结构?
  • sharepoint 2010 使用STSNavigate函数实现文件下载举例
  • 网站重定向用C语言实现iptables,ACL实现
  • php实现socket实现客户端和服务端数据通信源代码
  • 在linux下如何编程实现nslookup命令实现的IP地址和域名互相转换的功能?
  • flash AS3反射实现(describeType和getDefinitionByName)
  • 求在freebsd+Squid下实现pc上网的透明代理的实现方法!给出具体配置方法的高分谢!




  • 特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3