扫描局域网的所有ip

两种方法,直接上代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 扫描局域网内的ip
{
    class Program
    {
        static void Main(string[] args)
        {
            ScanIp scan = new ScanIp();
            scan.StartScanThread();
            Console.ReadKey();
            
        }    
    }

    public class ScanIp {
        public ScanIp(){
        }
        /// 
        /// 开启扫描Ip的线程
        /// 
        public void StartScanThread(){
            Thread thread = new Thread(EnumComputers);
            thread.Start();
        }

        //法一
       /// 
        /// //速度快,用户ping的方式,ping通了代表电脑在线,但也不是绝对,有些电脑是不给ping的
       /// 
        private void EnumComputers()
        {
            try
            {
                //获取本机的ip
                IPHostEntry here = Dns.GetHostByName(Dns.GetHostName());
                IPAddress localhostAddress = here.AddressList[0];
                string ip = localhostAddress.ToString();    //本机ip,如192.168.3.6
               
                string[] ipstr = ip.Split('.');            //ipstr[0]=192,ipstr[1]=168,ipstr[2]=3,ipstr[3]=6        
                //ip的前缀,即网段,如192.168.3.
                string  ipPrefix = ipstr[0] + "." + ipstr[1] + "." + ipstr[2] + ".";

                for (int i = 1; i <= 255; i++)
                {
                    
                    Ping myPing;
                    myPing = new Ping();
                    myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);

                    string pingIP = ipPrefix+ i.ToString();
                    myPing.SendAsync(pingIP, 1000, null);
                }
            }
            catch
            {
               
            }
        }

        private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            try
            {
                if (e.Reply.Status == IPStatus.Success)
                {
                   Console.WriteLine(e.Reply.Address.ToString() + "|" + Dns.GetHostByAddress(IPAddress.Parse(e.Reply.Address.ToString())).HostName);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("抛出异常");
                
            }

        }



        //法二,不过速度非常慢
        private void EnumComputersMethod2()
        {
            for (int i = 1; i <= 255; i++)
            {
                string scanIP = "192.168.3." + i.ToString();  //注意:192.168.3.0是网段,需要修改为要扫描的局域网网段,不过也可以像法一一样获取到本机ip,再切割为网段

                IPAddress myScanIP = IPAddress.Parse(scanIP);

                IPHostEntry myScanHost = null;

                try
                {
                    myScanHost = Dns.GetHostByAddress(myScanIP);
                }

                catch
                {
                    continue;
                }

                if (myScanHost != null)
                {
                    Console.WriteLine(scanIP + "|" + myScanHost.HostName);
                }
            }
        }

        /// 
        /// 与本主题无关的方法,可以了解一下,扫描本机某些端口是否开启
        /// 
        private void scanport()
        {
                try
                {
                    TcpClient client = new TcpClient();
                    IPAddress address = IPAddress.Parse("192.168.3.6");
                    for (int i = 139; i <=140; i++)
                    {
                        try
                        {
                            client.Connect(address, i);
                            Console.WriteLine("端口"+i+"是打开的");
                            //client = new System.Net.Sockets.TcpClient();
                        }
                        catch
                        {
                            Console.WriteLine("抛出异常");
                            Console.WriteLine("端口"+i+"没有打开");
                            continue;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("抛出异常");
                   
                }
        }
    }


  
}

 

你可能感兴趣的:(C#编程,c#,局域网ip,局域网,所有ip)