首页 > 开发 > 综合 > 正文

C#编写的多线程端口扫描程序

2020-02-03 13:45:31
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。 作者: 萧寒雪


  有幸购买到visual studio .net 的光盘,急忙安装一套,一用感觉非常好,所学写端口扫描程序一只,献给本版的朋友们:)
using system;;
using system.drawing;;
using system.collections;;
using system.componentmodel;;
using system.windows.forms;;
//增加的如下..
using system.data;;
using system.net.sockets;;
using system.net;;
using system.io;;
using system.text;;
using system.threading;;

namespace iportscan
{
/// 〈summary〉
/// summary description for form1.
/// 〈/summary〉
public class form1 : system.windows.forms.form
{
//该处放的是程序要用到的公共变量
public string scanhost = dns.gethostname();; //默认当前本机ip
public int32 tport = 0;; //当前连接端口编号
public int32 connstate = 0;; //扫描状态
public int portsum = 0 ;; //端口总计
public bool endthread = false;; //结束状态
public autoresetevent asyncopsaredone = new autoresetevent(false);;
private system.windows.forms.label label1;;
private system.windows.forms.textbox txthostname;;
private system.windows.forms.button cmdexec;;
private system.windows.forms.listbox loglist;;
public system.windows.forms.checkedlistbox portlist;;
private system.windows.forms.label label2;;
private system.windows.forms.label label3;;
private system.windows.forms.numericupdown snum;;
private system.windows.forms.numericupdown enum;;
private system.windows.forms.checkbox showdie;;
private system.windows.forms.label label4;;
private system.windows.forms.button button1;;
private system.windows.forms.tooltip tooltip1;;
private system.windows.forms.statusbar statusbar1;;
private system.windows.forms.linklabel linklabel1;;
private system.windows.forms.linklabel linklabel2;;
private system.componentmodel.icontainer components;;
/*
此处省略了各个调用的控件的属性设置代码
*/
static void main()
{
application.run(new form1());;
}

private void cmdexec_click(object sender, system.eventargs e)
{
int32 startport = (int32)snum.value;;
int32 endport = (int32)enum.value;;
if(txthostname.text.length==0)
{
messagebox.show("请输入一个主机的名称吧!","系统提示");;
txthostname.text = scanhost.tostring();;
txthostname.focus();;
return;;
}
if(startport〉endport)
{
messagebox.show("错误,起始端口必须要小于结束的端口!","系统提示");;
startport = endport-1;;
snum.text = startport.tostring();;
snum.focus();;
return ;;
}

if(cmdexec.text=="&scan")
{
endthread= false;;
cmdexec.text= "&stop";;
}
else
{
endthread= true;;
cmdexec.text= "&scan";;
}

if(endthread!=true)
{
connstate = 0;;
portsum = 0;;
scanhost = txthostname.text;;
try
{
ipaddress ipaddr =(ipaddress)dns.resolve(scanhost).addresslist.getvalue(0);;
txthostname.text = ipaddr.tostring();;
}
catch
{
txthostname.focus();;
messagebox.show("请输入正确的主机地址,该地址dns无法解析","系统提示");;
return ;;
}
loglist.items.clear();;

for (int32 threadnum = startport;; threadnum 〈=endport;; threadnum++)
{
threadpool.queueuserworkitem(new waitcallback(startscan),threadnum);;
loglist.items.add ("扫描端口:" + 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();;
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表