14

How I can compare two IP address?

string ip1 = "123.123.123.123";
string ip2 = "124.124.124.124";

I need some like this:

if(ip1 == ip2)
{
   //true
}
Joey
  • 316,376
  • 76
  • 642
  • 652
user319854
  • 3,650
  • 11
  • 39
  • 44

6 Answers6

35

It seems System.Net.IPAddress defines it's own Equals override so this should work:

IPAddress ip1 = IPAddress.Parse("123.123.123.123");
IPAddress ip2 = IPAddress.Parse("124.124.124.124");

if(ip1.Equals(ip2))
{
    //...
}
Lee
  • 133,981
  • 18
  • 209
  • 268
  • @PhucNguyen - `IPAddress.Parse("172.16.0.150").Equals(IPAddress.Parse("172.16.1.216"))` returns false for me as expected. – Lee Jul 13 '17 at 07:53
19

The type IPAddress in the BCL supports equality and can be used for this purpose.

public static bool IsSameIPAddress(string ip1, string ip2) {
  IPAddress leftIP = IPAddress.Parse(ip1);
  IPAddress rightIP = IPAddress.Parse(ip2);
  return leftIP.Equals(rightIP);
}

Several people have wondered why a straight string comparison is not sufficient. The reason why is that an IP address can be legally represented in both base 10 and hexidecimal notation. So the same IP address can have more than 1 string representation.

For example

var left = "0x5.0x5.0x5.0x5";
var right = "5.5.5.5";
IsSameIPAddress(left,right); // true
left == right; // false
JaredPar
  • 673,544
  • 139
  • 1,186
  • 1,421
  • 4
    @JaredPar: `leftIP==rightIP` in your first code would return false for same IP address! It should be `return LeftIP.Equals(rightIP);` – KMån Apr 27 '10 at 15:49
  • 1
    @KMan, just noticed that myself and updated my post. Thanks for pointing it out! – JaredPar Apr 27 '10 at 15:49
  • The hex stuff was new to me. Interesting. It also gets worse with IPv6, since you don't *have* to shorten the addresses with `::` there. – Joey Apr 27 '10 at 15:50
  • I've never seen *dotted* hex format used. non-dotted hex yes (0x05050505), but never dotted hex. – Powerlord Apr 27 '10 at 15:53
  • @OMG Unicorns, both forms are legal at least in respect to what `IPAddress.Parse` supports. I'm not sure about the official IP standard off the top of my head though. – JaredPar Apr 27 '10 at 15:55
3

Check out Equals method on System.Net.IPAddress

Andrei Krasutski
  • 3,487
  • 1
  • 21
  • 33
Longball27
  • 656
  • 1
  • 8
  • 27
1

The IPAddress class (System.Net) has an overridden Equals method that will compare the addresses, not the object instances, which is what you want. String comparison here may be dangerous since it is possible for IP addresses to have more than one string representation. http://msdn.microsoft.com/en-us/library/system.net.ipaddress.equals%28v=VS.71%29.aspx

IPAddress.Parse(ip1).Equals(IPAddress.Parse(ip2))
Eric Mickelsen
  • 9,511
  • 2
  • 27
  • 41
1
IPAddress addr1 = IPAddress.Parse(ip1);
IPAddress addr2 = IPAddress.Parse(ip2);

return (addr1.Equals(addr2));
KMån
  • 9,776
  • 2
  • 29
  • 40
1

You can use this class to compare IpAddress :

http://www.codeproject.com/Articles/26550/Extending-the-IPAddress-object-to-allow-relative-c