How to compare ip addresses

How to compare the IP address stored in the Ip [0] array with the remote endpoint? Please help me.

+1
source share
6 answers

Something like this should work ...

var ips = new[] { IPAddress.Parse( "127.0.0.1"), IPAddress.Parse( "192.168.1.1"), IPAddress.Parse( "10.0.0.1" ) }; var ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0); if (ips[0].Equals(ep.Address)) { Console.WriteLine("Equal!"); } 
+11
source

I assume you got the IP address through

 System.Net.EndPoint ep = client.Client.RemoteEndPoint; System.Net.IPEndPoint ip = (System.Net.IPEndPoint)ep; 

If this is the case, you can simply compare through

 System.Net.IPEndPoint ip = (System.Net.IPEndPoint)ep; ip.ToString(); if(Ip[0] == ip.toString()); 
+2
source

All of the above options will work, but there is another option not mentioned here: Use the IpAddress GetAddressBytes method to get the address in bytes and compare them. This can be useful if you need to do other processing (e.g. computing if Ip is in an IP class or something like this).

+1
source

Well, you can just get them: ToString () and then compare them. Or you can iterate over the 4 numbers that IPV4 ip has and compare them.

0
source

Just compare each member of the structure.

-1
source

Source: https://habr.com/ru/post/1309030/


All Articles