获取局域网内所有在线IP

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Net.NetworkInformation;

namespace guangbo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_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());
            //显示本地主机名
            this.textBox1.Text = myHost.HostName.ToString();
            //显示本地主机的IP地址表
            int a = myHost.AddressList.Length;
            for (int i = 0; i < a; i++)
            {
                string b = myHost.AddressList[0].ToString();
                //this.richTextBox1.AppendText("本地主机IP地址->" + myHost.AddressList.ToString() + " \r");
                this.richTextBox1.AppendText("本地主机IP地址->" + b + " \r");
            }
        }
        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (this.textBox2.Text == null)
        {
            MessageBox.Show("请输入设备名称");
        }
        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.ToString() + " \r");
            richTextBox1.AppendText(textBox2.Text + "的IP地址是" + myDnsToIP.AddressList[i].ToString() + " \r");
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Thread thScan = new Thread(new ThreadStart(ScanTarget));
        thScan.Start();

    }
    private void ScanTarget()
    {
        string strIPAddress = this.numericUpDown1.Text + "." + this.numericUpDown2.Text + "." + this.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();
            if (!PingIpOrDomainName(strScanIPAdd))
            {
                continue;
            }
            //string strScanIPAdd = "192.168.31.158";
            //转换成IP地址
            IPAddress myScanIP;
            myScanIP = IPAddress.Parse(strScanIPAdd);
            try
            {
                //Dns.GetHostByAddress方法:根据IP地址获取DNS主机信息
                IPHostEntry myScanHost = Dns.GetHostByAddress(myScanIP);
                //获取主机的名称
                string strHostName = myScanHost.HostName.ToString();
                //richTextBox1.AppendText(strScanIPAdd + " ->" + strHostName + " \r");
                MessageBox.Show(strScanIPAdd + " ->" + strHostName + " \r");
            }
            catch (Exception error)
            {
                continue;
            }
        }
        return;
    }
    /// 
    /// 用于检查IP地址或域名是否可以使用TCP/IP协议访问(使用Ping命令),true表示Ping成功,false表示Ping失败 
    /// 
    /// 输入参数,表示IP地址或域名
    /// 
    public static bool PingIpOrDomainName(string strIpOrDName)
    {
        try
        {
            Ping objPingSender = new Ping();
            PingOptions objPinOptions = new PingOptions();
            objPinOptions.DontFragment = true;
            string data = "";
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            int intTimeout = 120;
            PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
            string strInfo = objPinReply.Status.ToString();
            if (strInfo == "Success")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception)
        {
            return false;
        }
    }

}

}

你可能感兴趣的:(获取局域网内所有在线IP)