C# 中获取主机的DNS域名(练习单线程,多线程,线程池)


1.项目结构图如下:

2.运行效果如下:

3.核心源代码:

Form1.cs中的源码(设计源码省略,由于单线程,多线程,线程池只有部分源码不同,将其合到一处)

[csharp]
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Net;  
  10. using System.Threading;  
  11. namespace ScanComputer  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public string start;//记录当前IP字符串   
  16.         public int n;//计数   
  17.         public DateTime dt1;//当前时间   
  18.         public Form1()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.         private void button1_Click(object sender, EventArgs e)  
  23.         {  
  24.             groupBox1.Enabled = false;  
  25.             button1.Enabled = false;  
  26.             listBox1.Items.Clear();  
  27.             int ipCount =n= (int)numericUpDown5.Value - (int)numericUpDown4.Value + 1;//计算总IP数量   
  28.             // 多线程   
  29.             //Thread[] scanthreads = new Thread[ipCount];   
  30.             //for (int i = 0; i < ipCount; i++)   
  31.             //{   
  32.             //  start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value+i);   
  33.             //  scan sc = new scan(this);   
  34.             //  sc.ip = start;   
  35.             //  scanthreads[i] = new Thread(sc.CheckComputer);   
  36.             //  scanthreads[i].Name = i.ToString();   
  37.             //  scanthreads[i].Start();   
  38.             //}   
  39.             //线成池   
  40.             //scan[] sc = new scan[ipCount];   
  41.             //dt1 = DateTime.Now;//记下当前时间   
  42.             //for (int i = 0; i < ipCount; i++)   
  43.             //{   
  44.             //    start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value + i);   
  45.             //    sc[i] = new scan(this);   
  46.             //    sc[i].ip = start;   
  47.             //    //ThreadPool.QueueUserWorkItem(new WaitCallback(sc[i].CheckComputer));   
  48.             //}   
  49.             //单线程   
  50.             scan sc = new scan(this);  
  51.             dt1 = DateTime.Now;//记下当前时间   
  52.             for (int i = 0; i < ipCount; i++)  
  53.             {  
  54.                 start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value + i);  
  55.                 sc.ip = start;  
  56.                 object o = null;  
  57.                 sc.CheckComputer(o);  
  58.             }  
  59.         }  
  60.         public delegate void GetComputerDnsDelegate(string strIP, string strHostName);//在一个线程中访问另一个线程(此处为main线程)的控件要使用委托   
  61.         public void addInfotoListbox(string strIP,string hostName)  
  62.         {  
  63.             if (listBox1.InvokeRequired)  
  64.             {  
  65.                 GetComputerDnsDelegate get = addInfotoListbox;  
  66.                 listBox1.Invoke(get, strIP, hostName);  
  67.             }  
  68.             else  
  69.             {  
  70.                 n--;  
  71.                 this.listBox1.Items.Add("IP地址----" + strIP + ",Dns域名---" + hostName+" ");  
  72.                 if (n == 0)//若n=0说明所有线程已经完成任务,输出总时间   
  73.                 {  
  74.                     DateTime dt2 = DateTime.Now;  
  75.                     TimeSpan ts = dt2 - dt1;  
  76.                     string str=string.Format("总共用了{0:0.00}毫秒" , ts.TotalMilliseconds);//精确到0.00毫秒   
  77.                     this.listBox1.Items.Add(str);  
  78.                     this.groupBox1.Enabled = true;  
  79.                     button1.Enabled = true;  
  80.                 }  
  81.             }  
  82.         }  
  83.     }  
  84. }  

scan.cs源代码

[csharp]
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6. using System.Windows.Forms;  
  7. using System.Threading;  
  8. namespace ScanComputer  
  9. {  
  10.     class scan  
  11.     {  
  12.         Form1 form;  
  13.         public string ip;  
  14.         IPAddress IP;  
  15.         public scan(Form1 form)  
  16.         {  
  17.             this.form = form;  
  18.         }  
  19.         public void CheckComputer(object obj)  
  20.         {  
  21.             try  
  22.             {  
  23.                 IP = IPAddress.Parse(ip);  
  24.             }  
  25.             catch  
  26.             {  
  27.                 MessageBox.Show("不合法的IP地址!");  
  28.             }  
  29.             try  
  30.             {  
  31.                 string hostname = Dns.GetHostEntry(IP).HostName;  
  32.                 form.addInfotoListbox(ip, hostname);  
  33.             }  
  34.             catch  
  35.             {  
  36.                 return;  
  37.             }  
  38.         }  
  39.     }  
  40. }  

相关内容