C # IP internal and external network judgment

Source: Internet
Author: User

<summary>
Determine if the IP address is an intranet IP address
</summary>
<param name= "ipAddress" >ip address string </param>
<returns></returns>
public static bool Isinnerip (String ipAddress)
{
BOOL Isinnerip = false;
Long Ipnum = Getipnum (ipAddress);
/**
Private Ip:a class 10.0.0.0-10.255.255.255
Class B 172.16.0.0-172.31.255.255
Class C 192.168.0.0-192.168.255.255
And, of course, 127 of this network segment is the loopback address.
**/
Long Abegin = Getipnum ("10.0.0.0");
Long aend = Getipnum ("10.255.255.255");
Long Bbegin = Getipnum ("172.16.0.0");
Long bEnd = Getipnum ("172.31.255.255");
Long Cbegin = Getipnum ("192.168.0.0");
Long cend = Getipnum ("192.168.255.255");
Isinnerip = Isinner (Ipnum, Abegin, aend) | | Isinner (Ipnum, Bbegin, bEnd) | | Isinner (Ipnum, Cbegin, cend) | | Ipaddress.equals ("127.0.0.1");
return isinnerip;
}
<summary>
Convert IP address to long number
</summary>
<param name= "ipAddress" >ip address string </param>
<returns></returns>
private static Long Getipnum (String ipAddress)
{
string[] IP = ipaddress.split ('. ');
Long a = Int. Parse (Ip[0]);
Long B = Int. Parse (ip[1]);
Long c = Int. Parse (ip[2]);
Long d = Int. Parse (Ip[3]);

Long Ipnum = A * * * * * * * * * * * * * * * * * * * + * * * * + * * + + D;
return ipnum;
}
<summary>
Determine if the IP address of the user is converted to long after the IP address of the intranet is in range
</summary>
<param name= "Userip" ></param>
<param name= "Begin" ></param>
<param name= "End" ></param>
<returns></returns>
private static bool Isinner (long userip, long begin, Long end)
{
return (Userip >= begin) && (Userip <= end);
}

C # IP internal and external network judgment

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.