• C#获取局域网中的所有正在使用的IP地址


    方法不是很好。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Text.RegularExpressions;
    
    namespace _12获取局域网中的所有IP地址
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                IPAddress startIP = InputIPAddress("请输入开始IP地址:");
                IPAddress endIP = InputIPAddress("请输入结束IP地址:");
    
                int startNum;
                int endNum;
                string str1;
                string str2;
                GetBound(startIP, out startNum, out str1);
                GetBound(endIP, out endNum, out str2);
    
                if (str1 != str2) 
                {
                    Console.WriteLine("输入的IP不在同一段。");
                    return;
                }
                for (int i = startNum; i < endNum; i++) 
                {
                    try
                    {
                        IPAddress ip = IPAddress.Parse(str1 + i);
                        Console.Write("[{0}] - [", ip);
                        IPHostEntry hostEntry = Dns.GetHostEntry(ip);
                        Console.WriteLine("{0}]", hostEntry.HostName);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + "]");
                    }
                }
    
                Console.ReadKey();
            }
    
            static void Scan(IPAddress startIP, IPAddress endIP) 
            {
    
            }
    
            static void GetBound(IPAddress ip, out int num, out string str) 
            {
                string reg = @"(?<str>d+.d+.d+.)(?<num>d+)";
                Match match = new Regex(reg).Match(ip.ToString());
                string numStr = match.Groups["num"].Value;
                num = Convert.ToInt32(numStr);
                str = match.Groups["str"].Value;
            }
    
            static IPAddress InputIPAddress(string msg) 
            {
                string msgTemp = msg;
                IPAddress ip = null;
                while (true) 
                {
                    Console.Clear();
                    Console.WriteLine(msg);
                    string ipStr = Console.ReadLine();
                    if (IPAddress.TryParse(ipStr, out ip))
                    {
                        break;
                    }
                    else 
                    {
                        msg = "输入的"" + ipStr + ""格式不正确," + msgTemp;
                    }
                }
                Console.Clear();
    
                return ip;
            }
        }
    }
    

      


  • 相关阅读:
    多尺度双边滤波及基于小波变换的非线性扩散
    yum安装CentOS7+nginx+php7.3+mysql5.7
    python学习之特殊魔法__getattr__,__getattribute__
    python学习之特殊魔法__get__,__set__,__delete__
    python学习之装饰器
    python学习之私有属性
    python学习之包装与授权
    python学习之生成器(generator)
    python学习之运用特殊方法,定制类
    python学习之创建迭代器对象
  • 原文地址:https://www.cnblogs.com/liqipeng/p/4576163.html
Copyright © 2020-2023  润新知