2013-07-30 69 views
0

是否存在針對特定範圍內地址的比較IP地址的任何方法。比較Ip值以確定是否在特定範圍內

IPAddress[] ips; 
ips = Dns.GetHostAddresses("www.xyz.com"); 
Console.WriteLine("GetHostAddresses({0}) returns:", "www.xyz.com"); 
foreach (IPAddress ip in ips) 
{ 
    Console.WriteLine(" {0}", ip); 
} 
Console.ReadLine(); 

IPS變量存儲IP價值。我想比較一下10.100.12.21和10.255.15.30。 我如何比較ips其他類型? 在比較ip範圍後,轉換爲ips值爲double。還是有其他想法?

回答

0

自己的實現,試試這個:

int[] maxIP = new int[] { 10, 255, 15, 30 }; 
    int[] minIP = new int[] { 10, 100, 12, 21 }; 
    char[] sep = new char[] { '.' }; 

    var ip = "10.100.16.21"; 

    string[] splitted = ip.Split(sep); 

    for (int i = 0; i < splitted.Length; i++) 
    { 
     if (int.Parse(splitted[i]) > maxIP[i]) 
     { 
      Console.WriteLine("IP greather than max"); 
      break; 
     } 
     else if (int.Parse(splitted[i]) < minIP[i]) 
     { 
      Console.WriteLine("IP less than min"); 
      break; 
     } 
    } 
0

使用等於:

static void Main(string[] args) 
{ 
    IPAddress a, b; 

    a = new IPAddress(new byte[] { 10, 100, 12, 21 }); 
    b = new IPAddress(new byte[] { 10, 100, 12, 21 }); 

    Console.WriteLine("Value is {0}", a.Equals(b)); 

    Console.ReadLine(); 
} 
+0

IPS = Dns.GetHostAddresses( 「www.xyz.com」) 我要比較的10.100之間IPS值.12.21和10.255.15.30 我需要這樣的 IF(IPS> 10.100.12.21 && 10.255.15.30 一) IPS和一個具有相同類型,但IDE不允許 這個比較 – dgn

+0

在這種方式,你必須自己寫一個,因爲它不應該確定何時應該比/小於 – 0699

相關問題