-2

I m using code

string host = System.Net.Dns.GetHostName();
string hostname = Dns.GetHostEntry(host).HostName;
IPHostEntry ipEntry = Dns.GetHostEntry(host);
IPAddress[] addr = ipEntry.AddressList;

On local its working fine and gives me "192.168.1.4" but on server side it gives "2002:cc5d:a178::cc5d:a178"

Mitesh Jain
  • 555
  • 4
  • 13
  • 40

3 Answers3

2

The first address 192.168.1.4 is an IPv4 and the second address 2002:cc5d:a178::cc5d:a178 is an IPv6 address. Sadly you cannot convert from IPv6 to IPv4 as detailed here: Problem Converting ipv6 to ipv4

Simple answer: Disable IPV6 on the server, or remove the IPV6 address of the server from the DNS entry.

There is not a magic IPV4<->IPV6 converter. They're completely different protocols, and addresses in one don't translate to the other. If you want to reliably retrieve the IPV4 address of the client, you need to make sure that the client connects over IPV4.

Update If you want to disable IPv6 (on a Windows server) follow the instructions in the Microsoft Support article: http://support.microsoft.com/kb/929852 but remember that IPv4 is slowly being replaced by IPv6 so you're probably better off using the server default IP resolution.

Community
  • 1
  • 1
Kane
  • 16,471
  • 11
  • 61
  • 86
  • how shall i disable IPV6 on the server ?? is there any code for that? – Mitesh Jain Jul 29 '13 at 13:32
  • Disabling ipv6 on the server is almost certainly not a good idea. The KB article above states explicitly "We do not recommend disabling ipV6", and you can probably get the information you need without such a radical step. – weloytty Jul 29 '13 at 14:45
1

youre getting >1 Ip back, and (assuming the server is really getting an ipv4 address) you can just look for addresses that are IPv4 as opposed to ipV6. Like

        string host = System.Net.Dns.GetHostName();
        string hostname = Dns.GetHostEntry(host).HostName;
        IPHostEntry ipEntry = Dns.GetHostEntry(host);
        IPAddress[] addr = ipEntry.AddressList;
        foreach (IPAddress a in addr)
            Console.WriteLine("{0}:{1}", a.AddressFamily, a.ToString());

Which will get you something along the lines of:

InterNetworkV6:fe80::c80b:d804:38c3:2734%17
InterNetworkV6:fe80::752d:9c4a:69fd:cb63%15
InterNetwork:169.254.80.80
InterNetwork:10.0.1.17
weloytty
  • 5,808
  • 5
  • 28
  • 35
  • IPAddress[] addr = ipEntry.AddressList; gives me addr[0]=fe80::c80b:d804:38c3:2734%17....addr[1]=fe80::752d:9c4a:69fd:cb63%15.....addr[2]=192.168.1.5......addr[3]=10.0.1.17...its working fine on my local host....but when running it on server addr[2] returns me some thing like 2002:cc5d:a178::cc5d:a178 – Mitesh Jain Jul 30 '13 at 06:40
  • if you do not have ipv4 setup at all...then you're probably out of luck. You can enable/disable it easily (a quick google found http://mscerts.programming4.us/windows_server/windows%20server%202008%20%20%20configuring%20ipv4%20and%20ipv6%20addressing.aspx) BUT this isnt something you should do at a whim or without knowing what is going on (i.e. why is ipv4 not enabled on the server? do you have DHCP setup for it? you need to get your network admins involved) – weloytty Jul 30 '13 at 12:00
0

You can use this.

using System;
using System.Net;

public class IPNetworking
{
  public static string GetIP4Address()
  {
    string IP4Address = String.Empty;

    foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    if (IP4Address != String.Empty)
    {
      return IP4Address;
    }

    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    return IP4Address;
  }

//call this function to get Ip

string ClientIP = IP4.GetIP4Address();
Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31