中国最大的web开发资源网站及技术社区,
c#第一个正式应用,asp.net追捕休整版!
这个程序,是前天我买到visual studio.net beta 2 中文版后编写的第一个正式程序,感觉只真是好极了!!
这个追捕的代码参考过飞刀的asp.net 追捕的文章,但他文章中是针对.net 框架beta1的,而beta2中好多的地方都修改调整了,顺便也给程序增加了列表ip的功能,也就是说主机的多个ip地址都可以列出来的.只是那个根据ip查到ip所在地的功能暂时没做,因为当时我用的机器是朋友的,而手头没有ip数据库:~(
static void main()
{
application.run(new form1());
}
private void form1_load(object sender, system.eventargs e)
{
string localhost = system.net.dns.gethostname();
int i = 0;
int n = dns.resolve(localhost.tostring()).addresslist.getlength(0);
lblipinfo.text = localhost.tostring()+",就是你啊!";
txthostname.text = dns.resolve(localhost.tostring()).addresslist.getvalue(0).tostring();
lblsystem.text = "待命中...";
lblhttp.text = "待命中...";
lblftp.text = "待命中...";
lbldns.text = "待命中...";
lbltelnet.text = "待命中...";
lblsmtp.text = "待命中...";
lblpop.text = "待命中...";
for(;i<n;i++)
{
iplistbox.items.add(dns.resolve(localhost.tostring()).addresslist.getvalue(i).tostring());
}
}
private void cmdexec_click(object sender, system.eventargs e)
{
ipaddress theip;
txthostname.focus();
if(txthostname.text.length==0)
{
messagebox.show("您可以输入要追捕的ip或者主机名称!但不能为空","系统提示");
return; //false
}
try
{
int i = 0;
int n = dns.resolve(txthostname.text.tostring()).addresslist.getlength(0);
iplistbox.items.clear();
for(;i<n;i++)
{
iplistbox.items.add(dns.resolve(txthostname.text.tostring()).addresslist.getvalue(i).tostring());
}
theip = (ipaddress)dns.resolve(txthostname.text.tostring()).addresslist.getvalue(0);
lblipinfo.text = theip.tostring();
if(check1.checked==true)
{//
lblsystem.text = "检测中...";
lblhttp.text = "检测中...";
lblftp.text = "检测中...";
lbldns.text = "检测中...";
lbltelnet.text = "检测中...";
lblsmtp.text = "检测中...";
lblpop.text = "检测中...";
//检查服务
detectservice(theip);
detectport(theip);
lblsystem.text = strsystem.tostring();
lblhttp.text = strhttp.tostring();
lblftp.text = strftp.tostring();
lbldns.text = strdns.tostring();
lbltelnet.text = strtelnet.tostring();
lblsmtp.text = strsmtp.tostring();
lblpop.text = strpop.tostring();
}
if(check2.checked==true)
{
//查ip 所在地区,未实现
}
}
catch
{
messagebox.show("这个主机无法连接哦!/r/n请重新输入一个正确的吧!","系统提示");
txthostname.text = "";
txthostname.focus();
lblipinfo.text = "无法连接或转换ip,所在地大概是外星球!";
lblsystem.text = "待命中...";
lblhttp.text = "待命中...";
lblftp.text = "待命中...";
lbldns.text = "待命中...";
lbltelnet.text = "待命中...";
lblsmtp.text = "待命中...";
lblpop.text = "待命中...";
}
return;
}
private void detectservice(ipaddress theip)
{
string tftp = tcpconnect(theip,21); //is ftp service
string ttelnet = tcpconnect(theip,23); //is telnet service
string tdns = tcpconnect(theip,43); //is dns service
string tpop = tcpconnect(theip,110); //is pop3 service
string tsmtp = tcpconnect(theip,25); //is mail smtp service
if(tftp!="none")strftp = tftp;
if(ttelnet!="none")strtelnet = ttelnet;
if(tdns!="none")strdns = tdns;
if(tpop!="none")strpop = tpop;
if(tsmtp!="none")strsmtp = tsmtp;
tcpclient tcp = new tcpclient();
tcp.connect(theip,80);
int thttpstart;
string thttp;
string tmsg = "post /index.htm http/1.1/r/n" +
"connection: keep-alive/r/n/r/n";
stream sm = tcp.getstream();
sm.write(encoding.default.getbytes(tmsg.tochararray()),0,tmsg.length);
streamreader sr = new streamreader(tcp.getstream(),encoding.default);
while(sr.peek()!=-1)
{
thttp= sr.readline();
thttpstart = thttp.indexof("server");
if(thttpstart!=-1)
{
strhttp = thttp;
break;
}
}
tcp.close();
}
private string tcpconnect(ipaddress theip,int port)
{
tcpclient tcp = new tcpclient();
streamreader sr;
string result;
try
{
checked
{
tcp.connect(theip,port);
sr = new streamreader(tcp.getstream().tostring(),encoding.default);
result = sr.readline().tostring();
tcp.close();
}
}
catch
{
result = "none";
}
return result;
}
private void detectport(ipaddress theip)
{
//is
if(strhttp.indexof("microsoft")!=-1)
{
strsystem = "windows";
if(strhttp.indexof("iis")!=-1)
{
strsystem="windows nt/2000";
if(strhttp.indexof("5.0")!=-1)
strsystem="windows 2000";
else
strsystem="windows nt";
}
}
else if(strhttp.indexof("apache")!=-1)
{
strsystem = "unix/linux";
if(strhttp.indexof("unix")!=-1)
strsystem="unix";
}
else
{
if(tcpconnect(theip,139)!="none")
strsystem="windows 98/me";
else if(tcpconnect(theip,1433)!="none")
strsystem="windows nt/2000";
else
strsystem="windows/linux/unix";
}
}
private void iplistbox_selectedindexchanged(object sender, system.eventargs e)
{
lblipinfo.text = iplistbox.text.tostring()+",所在地查询未实现!";
txthostname.text = iplistbox.text.tostring();
}
private void button1_click(object sender, system.eventargs e)
{
application.exit();
}
private void linklabel1_linkclicked(object sender, system.windows.forms.linklabellinkclickedeventargs e)
{
linklabel1.show();
}
完整的代码和编译过的程序:http://202.103.224.224/icools/bbs/non-cgi/usr/5_2.zip
记得一定要装 .net frameworksdk beta2
我是初学者,有问题一起研究!
by 萧寒雪(s.f.)