Question

I'm quite a beginner in C# and now I've this problem to recover the ip address of my localhost web.

CS:

LocalIp = Request.ServerVariables["REMOTE_ADDR"];

Response.Write("IP Address: " + LocalIp.ToString());
Response.End();

I wait 127.0.0.1 but in output I've:

IP Address: ::1

What shall I do? The version of IIS is 7.5 in windows 7. Thank in advance,

EDIT: Solved with take a look at the default Windows hosts file, at C:\Windows\System32\drivers\etc\hosts (open with notepad) In previous versions of Windows, we find the following active entry for "localhost"

127.0.0.1       localhost

But in Windows 7 and Windows 2008 R2, the hosts file is effectively empty. While there are both IPv4 and IPv6 entries for "localhost", both are disabled by being commented out.

# 127.0.0.1       localhost
# ::1             localhost

I've enabled and now all working.

127.0.0.1       localhost
Was it helpful?

Solution

That's an IPv6 address. If the connection was made with IPv6, then that is the value you'll get.

OTHER TIPS

Looks good. ::1 is the ipv6 way of saying 127.0.0.1 or localhost.

Actually the ::1 is the correct IPv6 address of your localhost. You were expecting the IPv4 version. Take a look at this article over at 4guysfromrolla.com:

https://web.archive.org/web/20211020102847/https://www.4guysfromrolla.com/articles/071807-1.aspx

Try this one. May be It will help you.

        IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
        foreach (IPAddress Addr in localIPs)
        {                
            if (Addr.AddressFamily == AddressFamily.InterNetwork)
            {
                Response.Write("IP Address: " + Addr );
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top