using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace 端口扫描器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class MainApp : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblAddr;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtAddr;
private System.Windows.Forms.TextBox txtStart;
private System.Windows.Forms.TextBox txtEnd;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button btnScan;
private System.Windows.Forms.ListBox lbResult;
//定义私有变量
private int port;
private string Addr;
private bool[] done =new bool[65536];
private int start;
private int end;
private Thread scanThread;
private bool OK;
private int i;
private System.Windows.Forms.Label lblStart;
private System.Windows.Forms.Label lblNow;
private System.Windows.Forms.Label lblStop;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public MainApp()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace 端口扫描器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class MainApp : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblAddr;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtAddr;
private System.Windows.Forms.TextBox txtStart;
private System.Windows.Forms.TextBox txtEnd;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button btnScan;
private System.Windows.Forms.ListBox lbResult;
//定义私有变量
private int port;
private string Addr;
private bool[] done =new bool[65536];
private int start;
private int end;
private Thread scanThread;
private bool OK;
private int i;
private System.Windows.Forms.Label lblStart;
private System.Windows.Forms.Label lblNow;
private System.Windows.Forms.Label lblStop;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public MainApp()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainApp());
}
#region 处理扫描事件=========================================
private void btnScan_Click(object sender, System.EventArgs e)
{
if (txtAddr.Text.Trim()=="")
{
MessageBox.Show("提示:请输入您要扫描的主机地址!","信息");
txtAddr.Focus();
return;
}
else if (txtStart.Text.Trim()=="") {
MessageBox.Show("提示:请输入您要扫描的开始端口!","信息");
txtStart.Focus();
return;
}
else if(txtEnd.Text.Trim()=="")
{
MessageBox.Show("提示:请输入您要扫描的结束端口!","信息");
txtEnd.Focus();
return;
}
else
{
lbResult.Items.Clear();
lbResult.Items.Add("端口扫描器 v1.0");
lbResult.Items.Add("");
lbResult.Items .Add("正在连接主机 ...");
IPHostEntry serverHe;
try
{
serverHe=Dns.GetHostByName(txtAddr.Text);
}
catch(Exception)
{
lbResult.Items .Add("连接主机的败 ...");
return;
} } Thread process=new Thread(new ThreadStart(PortScan));
process.Start();
progressBar1.Minimum=Int32.Parse(txtStart.Text);
progressBar1.Maximum =Int32.Parse(txtEnd.Text);
}
#endregion
#region 扫描过程=============================================
private void PortScan()
{
start=Int32.Parse(txtStart.Text);
end =Int32.Parse(txtEnd.Text);
if ((start>=0 && start<=65536) && (end>=0 && end<=65536))
{
lbResult.Items .Add("开始扫描..(可能需要请您等待几分钟)");
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("--------------------------------------------");
lbResult.Items.Add("扫描结束!");
DialogResult answer;
answer = MessageBox.Show("你要保存扫描报告吗?","信息",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if(answer == DialogResult.Yes)
{
string fname = ".\\"+txtAddr.Text+".txt";
FileInfo finfo = new FileInfo(fname);
//判断文件是否存在
if ( finfo.Exists)
{
//删除该文件
finfo.Delete();
}
for (int i=0;i<=lbResult.Items.Count-1;i++)
{
WriteRportFile(lbResult.Items[i].ToString());
}
MessageBox.Show("扫描报告保存完成,在程序目录下!","信息");
} }
else
{
MessageBox.Show("可用的端口范围为[0-65536]","提示");
}
}
private void Scan()
{
int portnow=port;
done[portnow]=true;
TcpClient objTCP=null;
try
{
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;
}
#endregion
#region 校验端口输入=========================================
private void txtStart_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if("0123456789".IndexOf(e.KeyChar) == -1 && e.KeyChar !=(char)8)
{
e.Handled = true ;
}
} private void txtEnd_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if("0123456789".IndexOf(e.KeyChar) == -1 && e.KeyChar !=(char)8)
{
e.Handled = true ;
}
}
#endregion
#region 将扫描报告写入文件文件===============================
public void WriteRportFile(String input)
{
//指定日志文件的目录
string fname = ".\\"+txtAddr.Text+".txt";
//定义文件信息对象
FileInfo finfo = new FileInfo(fname);
//创建只写文件流
using(FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter w = new StreamWriter(fs);
//设置写数据流的起始位置为文件流的末尾
w.BaseStream.Seek(0, SeekOrigin.End);
//写入... ”
w.Write(input+"\r\n");
w.Flush();
//关闭写数据流
w.Close();
}
} #endregion }
}
对了,是在<<突破C#编程实例五十讲>>中
我增加了输入校验IP,端口。增加了扫描报告!关键在于学习,希望有兴趣的朋友可以多加一些功能。
www.aspcool.com