老師要求寫的一個端口掃描器
圖:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071954.jpg)
源代碼:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System.Drawing;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System.Collections;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System.ComponentModel;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System.Windows.Forms;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System.Data;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System.Net;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System.Net.Sockets;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
using System.Threading;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071909.gif)
namespace scanports
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071903.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072042.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
/**//// <summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
/// Form1 的摘要說明。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
/// </summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
public class Form1 : System.Windows.Forms.Form
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.Label label1;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.TextBox txtip;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.Label label2;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.TextBox txtmin;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.TextBox txtmax;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.Label label3;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.ListBox lisstatus;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.Button btnexit;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.Button btnscan;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.Button btnpause;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.Windows.Forms.Button btncontinnue;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//定義一個線程
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private Thread t;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
/**//// <summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
/// 必需的設計器變量。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
/// </summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private System.ComponentModel.Container components = null;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
public Form1()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// Windows 窗體設計器支持所必需的
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
InitializeComponent();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
/**//// <summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
/// 清理所有正在使用的資源。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
/// </summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
protected override void Dispose( bool disposing )
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
if( disposing )
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
if (components != null)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
components.Dispose();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
base.Dispose( disposing );
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
Windows 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
/**//// <summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
/// 此方法的內容。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
/// </summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private void InitializeComponent()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label1 = new System.Windows.Forms.Label();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtip = new System.Windows.Forms.TextBox();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnscan = new System.Windows.Forms.Button();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.lisstatus = new System.Windows.Forms.ListBox();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label2 = new System.Windows.Forms.Label();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmin = new System.Windows.Forms.TextBox();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmax = new System.Windows.Forms.TextBox();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label3 = new System.Windows.Forms.Label();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnexit = new System.Windows.Forms.Button();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnpause = new System.Windows.Forms.Button();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btncontinnue = new System.Windows.Forms.Button();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.SuspendLayout();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// label1
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
&nbs//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label1.Location = new System.Drawing.Point(8, 24);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label1.Name = "label1";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label1.Size = new System.Drawing.Size(56, 16);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label1.TabIndex = 0;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label1.Text = "機器IP";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// txtip
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtip.Location = new System.Drawing.Point(56, 16);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtip.Name = "txtip";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtip.Size = new System.Drawing.Size(128, 21);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtip.TabIndex = 1;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtip.Text = "";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// btnscan
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnscan.Location = new System.Drawing.Point(192, 16);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnscan.Name = "btnscan";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnscan.TabIndex = 2;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnscan.Text = "掃描";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnscan.Click += new System.EventHandler(this.btnscan_Click);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// lisstatus
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.lisstatus.ItemHeight = 12;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.lisstatus.Location = new System.Drawing.Point(8, 112);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.lisstatus.Name = "lisstatus";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.lisstatus.ScrollAlwaysVisible = true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.lisstatus.Size = new System.Drawing.Size(256, 232);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.lisstatus.TabIndex = 3;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// label2
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label2.Location = new System.Drawing.Point(16, 48);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label2.Name = "label2";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label2.Size = new System.Drawing.Size(56, 16);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label2.TabIndex = 4;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label2.Text = "端口范圍";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// txtmin
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmin.Location = new System.Drawing.Point(80, 48);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmin.Name = "txtmin";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmin.Size = new System.Drawing.Size(32, 21);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmin.TabIndex = 5;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmin.Text = "";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// txtmax
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmax.Location = new System.Drawing.Point(152, 48);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmax.Name = "txtmax";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmax.Size = new System.Drawing.Size(32, 21);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmax.TabIndex = 6;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmax.Text = "";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
&nbs // label3
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label3.Location = new System.Drawing.Point(120, 48);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label3.Name = "label3";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label3.Size = new System.Drawing.Size(16, 16);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label3.TabIndex = 7;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.label3.Text = "到";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// btnexit
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnexit.Location = new System.Drawing.Point(192, 48);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnexit.Name = "btnexit";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnexit.TabIndex = 8;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnexit.Text = "退出";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnexit.Click += new System.EventHandler(this.btnexit_Click);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// btnpause
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnpause.Enabled = false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnpause.Location = new System.Drawing.Point(16, 80);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnpause.Name = "btnpause";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnpause.TabIndex = 9;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnpause.Text = "暫停";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btnpause.Click += new System.EventHandler(this.btnpause_Click);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// btncontinnue
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btncontinnue.Enabled = false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btncontinnue.Location = new System.Drawing.Point(112, 80);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btncontinnue.Name = "btncontinnue";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btncontinnue.TabIndex = 10;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btncontinnue.Text = "繼續";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.btncontinnue.Click += new System.EventHandler(this.btncontinnue_Click);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
// Form1
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.ClIEntSize = new System.Drawing.Size(280, 350);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.btncontinnue);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.btnpause);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.btnexit);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.label3);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.txtmax);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.txtmin);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.label2);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.lisstatus);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.btnscan);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.txtip);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Controls.Add(this.label1);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Name = "Form1";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Text = "端口掃描器";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.ResumeLayout(false);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
#endregion
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
/**//// <summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
/// 應用程序的主入口點。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
/// </summary>
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
[STAThread]
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
static void Main()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
Application.Run(new Form1());
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private void btnscan_Click(object sender, System.EventArgs e)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//清空列表
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
lisstatus.Items.Clear();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//相應的按鈕的顯示與關閉
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
btnpause.Enabled=true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
btnscan.Enabled=false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtip.Enabled=false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmax.Enabled=false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmin.Enabled=false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//調用線程
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
t=new Thread(new ThreadStart(scanport));
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
t.Start();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
void scanport()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
try
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
TcpClient client=new TcpClIEnt();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
IPAddress address=IPAddress.Parse(txtip.Text);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
for(int i=int.Parse(txtmin.Text);i<=int.Parse(txtmax.Text);i++)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
try
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//掃描制定的端口范圍
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
clIEnt.Connect(address,i);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
lisstatus.Items.Add("端口"+i+"是打開的");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
catch(SocketException)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
lisstatus.Items.Add("端口"+i+"是關閉的");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
catch(Exception ex)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
MessageBox.Show(ex.Message);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
finally
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//設置按鈕的狀態
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
btnscan.Enabled=true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtip.Enabled=true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmax.Enabled=true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.txtmin.Enabled=true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private void btnexit_Click(object sender, System.EventArgs e)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//關閉線程,並退出
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
t.Abort();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
this.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private void btnpause_Click(object sender, System.EventArgs e)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//暫停線程
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
btnpause.Enabled=false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
t.Suspend();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
btncontinnue.Enabled=true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
private void btncontinnue_Click(object sender, System.EventArgs e)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072081.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072004.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
//繼續線程
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
btncontinnue.Enabled=false;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
t.Resume();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072009.gif)
btnpause.Enabled=true;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311072007.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311071903.gif)
}