C # method to determine whether a given IP address is within a specified range

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.