Obtain the IPv4 address of the client on an IPv6-enabled Machine

Source: Internet
Author: User
Tags servervariables

Start from Windows VistaIPv6 is enabled by default.In the environment where both IPv4 and IPv6 coexist, IPv6 has a high priority. Therefore, the obtained IP address is in the IPv6 format. Fortunately, to solve this problem, you do not have to deliberately disable IPv6. The same problem is the parsing of localhost in the host file. The IPv6 address is returned by default. For details, refer to this article.ArticleReturning an IPv4 address in an IPv6-Enabled environment, which also containsCode. The Code modified is as follows:

Using system;
Using system. net;
Using system. Web;

Public class ipnetworking
{
/// <Summary>
/// Obtain the IPv4 address of the guest host
/// </Summary>
/// <Returns> </returns>
Public static string getclientipv4 ()
{
String IPv4 = string. empty;

Foreach (IPaddress IP in DNS. gethostaddresses (getclientip ()))
{
If (IP. addressfamily. tostring () = "Internetwork ")
{
IPv4 = IP. tostring ();
Break;
}
}

If (IPv4! = String. Empty)
{
Return IPv4;
}

// The original code uses the DNS. gethostname method to retrieve data from the server rather than the client.
// Use the DNS. gethostentry method to retrieve DNS records from the obtained IPv6 address,
// Determine whether it belongs to the IPv4 protocol one by one. If it is an IPv4 address.
Foreach (IPaddress IP in DNS. gethostentry (getclientip (). Addresslist)
// Foreach (IPaddress IP in DNS. gethostaddresses (DNS. gethostname ()))
{
If (IP. addressfamily. tostring () = "Internetwork ")
{
IPv4 = IP. tostring ();
Break;
}
}

Return IPv4;
}

/// <Summary>
/// Obtain the address of the guest host
/// </Summary>
Public static string getclientip ()
{
If (null = httpcontext. Current. Request. servervariables ["http_via"])
{
Return httpcontext. Current. Request. servervariables ["remote_addr"];
}
Else
{
Return httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
}
}
}

Replacing IPv4 with IPv6 is a trend. Without large-scale deployment of IPv6, the above Code can be saved in an IPv4-dominated environment.

Reprinted from: http://www.cnblogs.com/shanyou/archive/2011/10/09/2204333.html

Start from Windows VistaIPv6 is enabled by default.In the environment where both IPv4 and IPv6 coexist, IPv6 has a high priority. Therefore, the obtained IP address is in the IPv6 format. Fortunately, to solve this problem, you do not have to deliberately disable IPv6. The same problem is the parsing of localhost in the host file. The IPv6 address is returned by default. The specific instructions can be seen in this article returning an IPv4 address in an IPv6-Enabled environment, the article also has code, run up does not meet expectations, the corrected code is as follows:

Using system;
Using system. net;
Using system. Web;

Public class ipnetworking
{
/// <Summary>
/// Obtain the IPv4 address of the guest host
/// </Summary>
/// <Returns> </returns>
Public static string getclientipv4 ()
{
String IPv4 = string. empty;

Foreach (IPaddress IP in DNS. gethostaddresses (getclientip ()))
{
If (IP. addressfamily. tostring () = "Internetwork ")
{
IPv4 = IP. tostring ();
Break;
}
}

If (IPv4! = String. Empty)
{
Return IPv4;
}

// The original code uses the DNS. gethostname method to retrieve data from the server rather than the client.
// Use the DNS. gethostentry method to retrieve DNS records from the obtained IPv6 address,
// Determine whether it belongs to the IPv4 protocol one by one. If it is an IPv4 address.
Foreach (IPaddress IP in DNS. gethostentry (getclientip (). Addresslist)
// Foreach (IPaddress IP in DNS. gethostaddresses (DNS. gethostname ()))
{
If (IP. addressfamily. tostring () = "Internetwork ")
{
IPv4 = IP. tostring ();
Break;
}
}

Return IPv4;
}

/// <Summary>
/// Obtain the address of the guest host
/// </Summary>
Public static string getclientip ()
{
If (null = httpcontext. Current. Request. servervariables ["http_via"])
{
Return httpcontext. Current. Request. servervariables ["remote_addr"];
}
Else
{
Return httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
}
}
}

Replacing IPv4 with IPv6 is a trend. Without large-scale deployment of IPv6, the above Code can be saved in an IPv4-dominated environment.

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.