The example in this article describes how C # determines whether a given IP address is within a specified range. Share to everyone for your reference. The specific analysis is as follows:
For example, given an IP segment: 127.0.0.1 ~ 127.0.0.255, we want to determine whether a given IP address is in this section, you can first convert the IP address to an integer, and then the integer comparison size is very easy.
For example:
127.0.0.1 = 2130706433
127.0.0.255 = 2130706687
Judge:
127.0.1.253 = 2130706941
Whether the integer size is directly compared within this range can be
To convert an IP address into an integer:
public static long Ip2long (string IP) { string[] ipbytes; Double num = 0; if (!string. IsNullOrEmpty (IP)) { ipbytes = IP. Split ('. '); for (int i = ipbytes.length-1; I >= 0; i--) { num + = (int. Parse (Ipbytes[i])% * MATH.POW (3-i));} } return (long) num;}
Determines whether a given IP address is within the specified range:
Long start = Ip2long ("127.0.0.1"), Long end = Ip2long ("127.0.0.255"), Long ipAddress = Ip2long ("127.0.1.253"); bool InRange = (ipAddress >= start && ipAddress <= end); if (inrange) {//IP Address fits within range!}
I hope this article is helpful to everyone's C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # method to determine whether a given IP address is within a specified range
This address: http://www.paobuke.com/develop/c-develop/pbk23090.html
Related content image processing in WPF C # How to use Npoi to import Excel wpf/silverlight How to implement image local amplification how to use regular expressions to judge characters in C #
Example of a list usage instance in C # using the factory method pattern in design mode for C # Programming explain C # Implementation parsing Baidu weather data, RSS parsing Baidu News and how to get the city based on IP C # simple multithreading synchronization and priority usage examples
C # method to determine whether a given IP address is within a specified range