歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C# 中獲取主機的DNS域名(練習單線程,多線程,線程池)

C# 中獲取主機的DNS域名(練習單線程,多線程,線程池)

日期:2017/3/1 10:36:56   编辑:Linux編程
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. }
Copyright © Linux教程網 All Rights Reserved