c#课件:网段扫描
软源LOGO
讲述咱程序员自己的故事

位置:软源 »源码 »文章

c#课件:网段扫描

coolapp;2012-04-12 23:46:11;浏览:1995

网段扫描 :使用 vs2008 调试通过, 线程安全模式。

using System;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace 网段扫描程序 {
    public partial class frmMain : Form {
        public frmMain() {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e) { }

        private void button1_Click(object sender, EventArgs e) {
            IPHostEntry myHost = new IPHostEntry();
            try {
                // Dns.GetHostName()获取本地计算机的主机名
                // Dns.GetHostByName()获取指定 DNS 主机名的 DNS 信息
                //得到本地主机的DNS信息

                myHost = Dns.GetHostByName(Dns.GetHostName());

                //显示本地主机名
                textBox1.Text = myHost.HostName.ToString();

                //显示本地主机的IP地址表
                for (int i = 0; i < myHost.AddressList.Length; i++) {
                    string ip = myHost.AddressList[i].ToString();
                    richTextBox1.AppendText("本地主机IP地址->" + ip + "\r");
                }
            } catch (Exception error) {
                MessageBox.Show(error.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e) {
            IPHostEntry myDnsToIP = new IPHostEntry();
            //Dns.Resolve 方法: 将 DNS 主机名或以点分隔的四部分表示法格式的 //  IP 地址解析为 IPHostEntry实例
            myDnsToIP = Dns.Resolve(textBox2.Text.ToString());
            //显示此域名的IP地址的列表
            for (int i = 0; i < myDnsToIP.AddressList.Length; i++) {
                richTextBox1.AppendText(textBox2.Text + "的IP地址是" + myDnsToIP.AddressList[i].ToString() + "\r");
            }
        }

        private void button3_Click(object sender, EventArgs e) {
            richTextBox1.Clear();
            //Thread 类: 创建并控制线程
            Thread thScan = new Thread(new ThreadStart(ScanTarget));
            //Thread.Start 方法:启动线程
            thScan.Start();
        }

        private void ScanTarget() {
            //构造IP地址的31-8BIT 位,也就是固定的IP地址的前段

            // numericUpDown1是定义的System.Windows.Forms.NumericUpDown控件

            string strIPAddress = numericUpDown1.Text + "." + numericUpDown2.Text + "." + numericUpDown3.Text + ".";
            //开始扫描地址
            int nStrat = Int32.Parse(numericUpDown4.Text);
            //终止扫描地址

            int nEnd = Int32.Parse(numericUpDown5.Text);
            //扫描的操作
            for (int i = nStrat; i <= nEnd; i++) {
                string strScanIPAdd = strIPAddress + i.ToString();
                //转换成IP地址
                IPAddress myScanIP = IPAddress.Parse(strScanIPAdd);
                try {//你可以加入自已的,增强功能

                    // Dns.GetHostByAddress 方法: 根据 IP 地
                    //址获取 DNS 主机信息。
                    IPHostEntry myScanHost =
                    Dns.GetHostByAddress(myScanIP);
                    //获取主机的名
                    string strHostName = myScanHost.HostName.ToString();
                    SetRichBoxTxt(strScanIPAdd + "->" + strHostName + "\r");
                } catch (Exception error) {
                    MessageBox.Show(error.Message);
                }
            }
        }
        delegate void SetTextCallback(string text);

     &nbs p;  private void SetRichBoxTxt(string text) {
            if (this.richTextBox1.InvokeRequired) {
                SetTextCallback d = new SetTextCallback(SetRichBoxTxt);
                this.Invoke(d, new object[] { text });
            } else {
                this.richTextBox1.Text = text;
            }
        }
    }
}
 

课件下载.rar

分享到:
(转载→)