Why when I use IPAddress.Any.Equals(IPAddress.Parse("127.0.0.1") it returns false?
And how can I compare two IPAdresses to get true?
Asked
Active
Viewed 61 times
-5
GulgDev
- 1
- 1
-
4Per the docs, "the `Any` field is equivalent to 0.0.0.0 in dotted-quad notation", which is clearly not `127.0.0.1`. `IPAddress.Equals` does do what it's supposed to for addresses that are actually equal. – Jeroen Mostert Apr 13 '22 at 11:10
-
Does this answer your question? [compare two ip with C#](https://stackoverflow.com/questions/2722598/compare-two-ip-with-c-sharp) – Luuk Apr 13 '22 at 11:20
1 Answers
1
IPAddress.Any represents 0.0.0.0. See System.Net.IPAddress. So it does of course not match the IPAddress 127.0.0.1.
Gabriel
- 166
- 1
- 6
-
OK, thank you. But how can I get true when comparing IPAddress,Any and any other ip address? – GulgDev Apr 13 '22 at 11:15
-
1
-
@GulgDev: it's not clear why you think that's useful. The only address that's equal to `IPAddress.Any` is `IPAddress.Any`, while the answer to the question "is this IP address covered by `IPAddress.Any` as a listening address" is always "yes". You may be thinking of testing IP addresses against masks instead, but that has nothing to do with `Any`. For "all local addresses", you can look at something like `NetworkInterface.GetAllNetworkInterfaces()` (and the `UnicastAddresses` within). – Jeroen Mostert Apr 13 '22 at 11:24