quot; + threadNum.ToString());
}
}
}
public void Startscan(Object state)
{
Int32 port = (Int32) state;
string tMsg = "";
string getData = "";
int lindex = 0;
int eindex = 0;
connState++; //判断线程数目
if(endThread==true)
{
if(connState==((Int32)eNum.Value-(Int32)sNum.Value))
{
cmdExec.Text = "&Scan";
logList.Items.Add ("扫描完毕!");
}
else
{
cmdExec.Text = "&Stop";
logList.Items.Add ("正在停止对"+port.ToString()+"端口的扫描线程");
}
logList.Items.Add("结束线程:"+port.ToString());
asyncOpsAreDone.Close();
}
else
{
try
{
TcpClient tcp = new TcpClient();
tcp.Connect(scanHost,port);
//该处如果建立连接错误的话,将不执行下面的代码..
portSum ++;
lindex = portList.Items.Add(port.ToString() + "端口开放",false);
portList.SelectedIndex=lindex;
Stream sm = tcp.GetStream();
sm.Write(Encoding.Default.GetBytes(tMsg.ToCharArray()),0,tMsg.Length);
StreamReader sr = new StreamReader(tcp.GetStream(),Encoding.Default);
getData = sr.ReadLine();
if(lindex!=0&&getData.Length!=0)
{
tMsg = " +-" + port.ToString() + "端口数据:"+getData.ToString();
eindex = portList.Items.Add(tMsg); //插入一条信息记录
portList.Items.Insert(lindex+1,tMsg);
portList.Items.RemoveAt(eindex);
}
sr.Close();
sm.Close();
tcp.Close();
}
catch
{
//显示坏死的端口
if(showdie.Checked==true)
{
portList.Items.Add(port.ToString()+"端口无法连接,回传数据为空");
}
}
finally
{
Thread.Sleep(0);
logList.Items.Add("结束线程:"+port.ToString());
asyncOpsAreDone.Close();
statusBar1.Text = "端口总计:"+portSum.ToString() ;
if(connState==((Int32)eNum.Value-(Int32)sNum.Value))
{
cmdExec.Text = "&Scan";
}
}
}
}
private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
总结:基于.Net 框架开发
程序确实简单方便,并且功能强大。
下载代码与执行文件:http://202.103.224.224/icools/bbs/non-cgi/usr/5_5.zip