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

1.项目结构图如下:

2.运行效果如下:

3.核心源代码:

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

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源代码

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; } } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值