老師要求寫的一個端口掃描器
圖:

源代碼:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Net;

using System.Net.Sockets;

using System.Threading;


namespace scanports


...{


/**//// <summary>

/// Form1 的摘要說明。

/// </summary>

public class Form1 : System.Windows.Forms.Form


...{

private System.Windows.Forms.Label label1;

private System.Windows.Forms.TextBox txtip;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.TextBox txtmin;

private System.Windows.Forms.TextBox txtmax;

private System.Windows.Forms.Label label3;

private System.Windows.Forms.ListBox lisstatus;

private System.Windows.Forms.Button btnexit;

private System.Windows.Forms.Button btnscan;

private System.Windows.Forms.Button btnpause;

private System.Windows.Forms.Button btncontinnue;

//定義一個線程

private Thread t;


/**//// <summary>

/// 必需的設計器變量。

/// </summary>

private System.ComponentModel.Container components = null;


public Form1()


...{

//

// Windows 窗體設計器支持所必需的

//

InitializeComponent();


//

// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼

//

}



/**//// <summary>

/// 清理所有正在使用的資源。

/// </summary>

protected override void Dispose( bool disposing )


...{

if( disposing )


...{

if (components != null)


...{

components.Dispose();

}

}

base.Dispose( disposing );

}



Windows 窗體設計器生成的代碼#region Windows 窗體設計器生成的代碼


/**//// <summary>

/// 設計器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內容。

/// </summary>

private void InitializeComponent()


...{

this.label1 = new System.Windows.Forms.Label();

this.txtip = new System.Windows.Forms.TextBox();

this.btnscan = new System.Windows.Forms.Button();

this.lisstatus = new System.Windows.Forms.ListBox();

this.label2 = new System.Windows.Forms.Label();

this.txtmin = new System.Windows.Forms.TextBox();

this.txtmax = new System.Windows.Forms.TextBox();

this.label3 = new System.Windows.Forms.Label();

this.btnexit = new System.Windows.Forms.Button();

this.btnpause = new System.Windows.Forms.Button();

this.btncontinnue = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// label1

&nbs//

this.label1.Location = new System.Drawing.Point(8, 24);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(56, 16);

this.label1.TabIndex = 0;

this.label1.Text = "機器IP";

//

// txtip

//

this.txtip.Location = new System.Drawing.Point(56, 16);

this.txtip.Name = "txtip";

this.txtip.Size = new System.Drawing.Size(128, 21);

this.txtip.TabIndex = 1;

this.txtip.Text = "";

//

// btnscan

//

this.btnscan.Location = new System.Drawing.Point(192, 16);

this.btnscan.Name = "btnscan";

this.btnscan.TabIndex = 2;

this.btnscan.Text = "掃描";

this.btnscan.Click += new System.EventHandler(this.btnscan_Click);

//

// lisstatus

//

this.lisstatus.ItemHeight = 12;

this.lisstatus.Location = new System.Drawing.Point(8, 112);

this.lisstatus.Name = "lisstatus";

this.lisstatus.ScrollAlwaysVisible = true;

this.lisstatus.Size = new System.Drawing.Size(256, 232);

this.lisstatus.TabIndex = 3;

//

// label2

//

this.label2.Location = new System.Drawing.Point(16, 48);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(56, 16);

this.label2.TabIndex = 4;

this.label2.Text = "端口范圍";

//

// txtmin

//

this.txtmin.Location = new System.Drawing.Point(80, 48);

this.txtmin.Name = "txtmin";

this.txtmin.Size = new System.Drawing.Size(32, 21);

this.txtmin.TabIndex = 5;

this.txtmin.Text = "";

//

// txtmax

//

this.txtmax.Location = new System.Drawing.Point(152, 48);

this.txtmax.Name = "txtmax";

this.txtmax.Size = new System.Drawing.Size(32, 21);

this.txtmax.TabIndex = 6;

this.txtmax.Text = "";

//

&nbs // label3

//

this.label3.Location = new System.Drawing.Point(120, 48);

this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(16, 16);

this.label3.TabIndex = 7;

this.label3.Text = "到";

//

// btnexit

//

this.btnexit.Location = new System.Drawing.Point(192, 48);

this.btnexit.Name = "btnexit";

this.btnexit.TabIndex = 8;

this.btnexit.Text = "退出";

this.btnexit.Click += new System.EventHandler(this.btnexit_Click);

//

// btnpause

//

this.btnpause.Enabled = false;

this.btnpause.Location = new System.Drawing.Point(16, 80);

this.btnpause.Name = "btnpause";

this.btnpause.TabIndex = 9;

this.btnpause.Text = "暫停";

this.btnpause.Click += new System.EventHandler(this.btnpause_Click);

//

// btncontinnue

//

this.btncontinnue.Enabled = false;

this.btncontinnue.Location = new System.Drawing.Point(112, 80);

this.btncontinnue.Name = "btncontinnue";

this.btncontinnue.TabIndex = 10;

this.btncontinnue.Text = "繼續";

this.btncontinnue.Click += new System.EventHandler(this.btncontinnue_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClIEntSize = new System.Drawing.Size(280, 350);

this.Controls.Add(this.btncontinnue);

this.Controls.Add(this.btnpause);

this.Controls.Add(this.btnexit);

this.Controls.Add(this.label3);

this.Controls.Add(this.txtmax);

this.Controls.Add(this.txtmin);

this.Controls.Add(this.label2);

this.Controls.Add(this.lisstatus);

this.Controls.Add(this.btnscan);

this.Controls.Add(this.txtip);

this.Controls.Add(this.label1);

this.Name = "Form1";

this.Text = "端口掃描器";

this.ResumeLayout(false);


}

#endregion



/**//// <summary>

/// 應用程序的主入口點。

/// </summary>

[STAThread]

static void Main()


...{

Application.Run(new Form1());

}


private void btnscan_Click(object sender, System.EventArgs e)


...{

//清空列表

lisstatus.Items.Clear();

//相應的按鈕的顯示與關閉

btnpause.Enabled=true;

btnscan.Enabled=false;

this.txtip.Enabled=false;

this.txtmax.Enabled=false;

this.txtmin.Enabled=false;

//調用線程

t=new Thread(new ThreadStart(scanport));

t.Start();

}

void scanport()


...{

try


...{

TcpClient client=new TcpClIEnt();

IPAddress address=IPAddress.Parse(txtip.Text);

for(int i=int.Parse(txtmin.Text);i<=int.Parse(txtmax.Text);i++)


...{

try


...{

//掃描制定的端口范圍

clIEnt.Connect(address,i);

lisstatus.Items.Add("端口"+i+"是打開的");

}

catch(SocketException)


...{

lisstatus.Items.Add("端口"+i+"是關閉的");

}

}

}

catch(Exception ex)


...{

MessageBox.Show(ex.Message);

}

finally


...{

//設置按鈕的狀態

btnscan.Enabled=true;

this.txtip.Enabled=true;

this.txtmax.Enabled=true;

this.txtmin.Enabled=true;

}

}


private void btnexit_Click(object sender, System.EventArgs e)


...{

//關閉線程,並退出

t.Abort();

this.Close();

}


private void btnpause_Click(object sender, System.EventArgs e)


...{

//暫停線程

btnpause.Enabled=false;

t.Suspend();

btncontinnue.Enabled=true;

}


private void btncontinnue_Click(object sender, System.EventArgs e)


...{

//繼續線程

btncontinnue.Enabled=false;

t.Resume();

btnpause.Enabled=true;

}

}

}