中國最大的web開發(fā)資源網(wǎng)站及技術(shù)社區(qū),
c#第一個(gè)正式應(yīng)用,asp.net追捕休整版! 
 這個(gè)程序,是前天我買到visual studio.net beta 2 中文版后編寫的第一個(gè)正式程序,感覺只真是好極了!! 
  
 這個(gè)追捕的代碼參考過飛刀的asp.net 追捕的文章,但他文章中是針對(duì).net 框架beta1的,而beta2中好多的地方都修改調(diào)整了,順便也給程序增加了列表ip的功能,也就是說主機(jī)的多個(gè)ip地址都可以列出來的.只是那個(gè)根據(jù)ip查到ip所在地的功能暫時(shí)沒做,因?yàn)楫?dāng)時(shí)我用的機(jī)器是朋友的,而手頭沒有ip數(shù)據(jù)庫:~( 
  
 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或者主機(jī)名稱!但不能為空","系統(tǒng)提示"); 
 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 = "檢測(cè)中..."; 
 lblhttp.text = "檢測(cè)中..."; 
 lblftp.text = "檢測(cè)中..."; 
 lbldns.text = "檢測(cè)中..."; 
 lbltelnet.text = "檢測(cè)中..."; 
 lblsmtp.text = "檢測(cè)中..."; 
 lblpop.text = "檢測(cè)中..."; 
 //檢查服務(wù) 
 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 所在地區(qū),未實(shí)現(xiàn) 
 } 
 } 
 catch 
 { 
 messagebox.show("這個(gè)主機(jī)無法連接哦!/r/n請(qǐng)重新輸入一個(gè)正確的吧!","系統(tǒng)提示"); 
 txthostname.text = ""; 
 txthostname.focus(); 
 lblipinfo.text = "無法連接或轉(zhuǎn)換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()+",所在地查詢未實(shí)現(xiàn)!"; 
 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 
  
 我是初學(xué)者,有問題一起研究! 
  
 by 蕭寒雪(s.f.)