国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#端口掃描

2024-07-21 02:26:52
字體:
來源:轉載
供稿:網友
菜鳥學堂:

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 = new label();
  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();
  }

 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 七台河市| 景东| 新和县| 海安县| 宝丰县| 长沙县| 柯坪县| 乌拉特后旗| 双牌县| 大渡口区| 浦东新区| 承德市| 湖口县| 泰宁县| 七台河市| 大同市| 高阳县| 沿河| 贡觉县| 南郑县| 内黄县| 罗甸县| 平阴县| 平谷区| 淅川县| 乌拉特后旗| 玉树县| 弋阳县| 台江县| 建阳市| 南京市| 海安县| 黄冈市| 克拉玛依市| 江都市| 河池市| 五台县| 宁陵县| 河源市| 清新县| 盐边县|