C# 中获取主机的DNS域名(练习单线程,多线程,线程池)
C# 中获取主机的DNS域名(练习单线程,多线程,线程池)
1.项目结构图如下:2.运行效果如下:
3.核心源代码:
Form1.cs中的源码(设计源码省略,由于单线程,多线程,线程池只有部分源码不同,将其合到一处)
[csharp]- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Threading;
- namespace ScanComputer
- {
- public partial class Form1 : Form
- {
- public string start;//记录当前IP字符串
- public int n;//计数
- public DateTime dt1;//当前时间
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- groupBox1.Enabled = false;
- button1.Enabled = false;
- listBox1.Items.Clear();
- int ipCount =n= (int)numericUpDown5.Value - (int)numericUpDown4.Value + 1;//计算总IP数量
- // 多线程
- //Thread[] scanthreads = new Thread[ipCount];
- //for (int i = 0; i < ipCount; i++)
- //{
- // start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value+i);
- // scan sc = new scan(this);
- // sc.ip = start;
- // scanthreads[i] = new Thread(sc.CheckComputer);
- // scanthreads[i].Name = i.ToString();
- // scanthreads[i].Start();
- //}
- //线成池
- //scan[] sc = new scan[ipCount];
- //dt1 = DateTime.Now;//记下当前时间
- //for (int i = 0; i < ipCount; i++)
- //{
- // start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value + i);
- // sc[i] = new scan(this);
- // sc[i].ip = start;
- // //ThreadPool.QueueUserWorkItem(new WaitCallback(sc[i].CheckComputer));
- //}
- //单线程
- scan sc = new scan(this);
- dt1 = DateTime.Now;//记下当前时间
- for (int i = 0; i < ipCount; i++)
- {
- start = numericUpDown1.Value + "." + numericUpDown2.Value + "." + numericUpDown3.Value + "." + (numericUpDown4.Value + i);
- sc.ip = start;
- object o = null;
- sc.CheckComputer(o);
- }
- }
- public delegate void GetComputerDnsDelegate(string strIP, string strHostName);//在一个线程中访问另一个线程(此处为main线程)的控件要使用委托
- public void addInfotoListbox(string strIP,string hostName)
- {
- if (listBox1.InvokeRequired)
- {
- GetComputerDnsDelegate get = addInfotoListbox;
- listBox1.Invoke(get, strIP, hostName);
- }
- else
- {
- n--;
- this.listBox1.Items.Add("IP地址----" + strIP + ",Dns域名---" + hostName+" ");
- if (n == 0)//若n=0说明所有线程已经完成任务,输出总时间
- {
- DateTime dt2 = DateTime.Now;
- TimeSpan ts = dt2 - dt1;
- string str=string.Format("总共用了{0:0.00}毫秒" , ts.TotalMilliseconds);//精确到0.00毫秒
- this.listBox1.Items.Add(str);
- this.groupBox1.Enabled = true;
- button1.Enabled = true;
- }
- }
- }
- }
- }
scan.cs源代码
[csharp]- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Windows.Forms;
- using System.Threading;
- namespace ScanComputer
- {
- class scan
- {
- Form1 form;
- public string ip;
- IPAddress IP;
- public scan(Form1 form)
- {
- this.form = form;
- }
- public void CheckComputer(object obj)
- {
- try
- {
- IP = IPAddress.Parse(ip);
- }
- catch
- {
- MessageBox.Show("不合法的IP地址!");
- }
- try
- {
- string hostname = Dns.GetHostEntry(IP).HostName;
- form.addInfotoListbox(ip, hostname);
- }
- catch
- {
- return;
- }
- }
- }
- }
评论暂时关闭