• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • C#編寫的多線程端口掃描程序

    發表于:2007-05-25來源:作者:點擊數: 標簽:線程程序寫的作者掃描
    作者: 蕭寒雪 有幸購買到Visual studio .net 的光盤,急忙安裝一套,一用感覺非常好,所學寫端口掃描程序一只,獻給本版的朋友們:) using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System. Windows .
    作者: 蕭寒雪

      有幸購買到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();
    }

    總結:基于.Net 框架開發程序確實簡單方便,并且功能強大。
    下載代碼與執行文件:http://202.103.224.224/icools/bbs/non-cgi/usr/5_5.zip

    原文轉自:http://www.kjueaiud.com

    評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>