2023年3月6日

廣播ip 計算

 


//廣播ip計算

 public class IP_Object
    {
 
        public String ip = "";
 
        public String mask = "";
 
 
        public String GetBroadcastAddress()
        {
 
            IPAddress address= IPAddress.Parse(ip.ToString ());
            IPAddress mask_value = IPAddress.Parse(mask.ToString());
 
            uint ipAddress = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
            uint ipMaskV4 = BitConverter.ToUInt32(mask_value.GetAddressBytes(), 0);
            uint broadCastIpAddress = ipAddress | ~ipMaskV4;
            IPAddress temp = new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
 
            return temp.ToString();
        }
 
        public override String ToString()
        {
            return ip;
        }
 
    }


取得local ip
   public static List<IP_Object> GetLocalIPAddress()
        {
 
            List<IP_Object> list = new List<IP_Object>();
 
            NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface Interface in Interfaces)
            {
                if (Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
                if (Interface.OperationalStatus != OperationalStatus.Up) continue;
                UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
 
                foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
                {
                    String ip = UnicatIPInfo.Address.ToString();
                    String mask=UnicatIPInfo.IPv4Mask.ToString();
 
 
                        String[] table = ip.Split(".");
                        if (table.Length != 4) continue;                             
                        String first = table[0];
                        String second = table[1];
                        int first_value;
                        int second_value;
                         if (int.TryParse(first, out first_value)==false)               
                            continue;
                        if (int.TryParse(second, out second_value)==false)
                            continue;
 
                         if (first_value == 10)
                        {
                            IP_Object ip_object=new IP_Object();
                            ip_object.ip = ip;
                            ip_object.mask = mask;                
                            list.Add(ip_object);
                        } else if (first_value == 192 && second_value == 168)
                        {
                            IP_Object ip_object = new IP_Object();
                            ip_object.ip = ip;
                            ip_object.mask = mask;
                            list.Add(ip_object);
                        }
                        else if (first_value == 172 && second_value >=16 && second_value<=31)
                        {
                            IP_Object ip_object = new IP_Object();
                            ip_object.ip = ip;
                            ip_object.mask = mask;
                            list.Add(ip_object);
                        }
 
                }
            }
 
            return list;
 
        }


沒有留言:

張貼留言