Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
2.52/5 (4 votes)
See more:
i need to get user ipaddress....i use below code

C#
private string GetIP()
    {
        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();

        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

        IPAddress[] addr = ipEntry.AddressList;

        return addr[addr.Length - 1].ToString();

    }


it worked in local site...But after publish my site it returns ipaddress in the below format:
"2001:0:4137:9e76:3c60:3e18:935c:505"
i don't know my mistake.. How can i get ipaddress...
if anyone know my mistake explain me..


Thanks n advance..
Posted
Updated 21-May-20 8:18am
Comments
Tien Nhat 14-Nov-15 7:35am    
thanks
ritzshaani 31-Mar-16 6:35am    
Actually, I am trying to know IP address of PC which is behind a PUBLIC IP , the .NET code returns the IP address of server instead of client. Can you help me to know any method in ASp.net to know the actual IP ,e.g.10.183....?

Here is the solution to get all valid IP4 Address list
C#
public static IEnumerable<string> GetAddresses()
{
     var host = Dns.GetHostEntry(Dns.GetHostName());
     return (from ip in host.AddressList where ip.AddressFamily == AddressFamily.InterNetwork select ip.ToString()).ToList();
}
</string>

on the above solution AddressFamily.InterNetwork filters the IP4 address from AddressFamily.

If you want your system valid IP address, I personally suggest below solution

C#
public static IPAddress GetIPAddress(string hostName)
{
    Ping ping = new Ping();
    var replay = ping.Send(hostName);

    if (replay.Status == IPStatus.Success)
    {
        return replay.Address;
    }
    return null;
 }

public static void Main()
{
    Console.WriteLine("Local IP Address: " + GetIPAddress(Dns.GetHostName()));
    Console.WriteLine("Google IP:" + GetIPAddress("google.com");
    Console.ReadLine();
}
 
Share this answer
 
Comments
Renju Vinod 14-Mar-13 3:22am    
+5
King Fisher 22-Apr-14 9:02am    
its perfect
Use
C#
using System;
using System.Net;

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

    foreach (IPAddress IPA in Dns.GetHostAddresses(Request.ServerVariables["REMOTE_ADDR"].ToString())))
    {
      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;
  }
}
 
Share this answer
 
v3
Comments
Muthu Vinoth Kumar 7-Jun-12 2:23am    
it return 127.0.0.1
uspatel 7-Jun-12 2:24am    
What is your IP?
Muthu Vinoth Kumar 7-Jun-12 2:26am    
191.186.1.52
uspatel 7-Jun-12 2:39am    
try updated answer
Muthu Vinoth Kumar 7-Jun-12 2:31am    
second foreach coding return my ip correctly...
whats differece b/w first foreach and second foreach
hi,
try this, it will return your IP Address
C#
string ip = "";
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();

IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

IPAddress[] addr = ipEntry.AddressList;

ip = addr[2].ToString();
 
Share this answer
 
Comments
Nijel Sabra 22-Mar-13 2:38am    
Hi coder,

Dns.GetHostEntry(strHostName); will returns 1) AddressList 2)Aliases and 3)HostName
and Here which "IPAddress[] addr = ipEntry.AddressList" is taken, but that IPAddress[] contains only two index position ,there will araise IndexOutofRangeException
So plz use this
ip = addr[2].ToString(); or
ip = addr[addr.Length-1].ToString();

Thanks,
Nijel
C#
private string GetIP()
    {
        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();

        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

        string ipaddress =convert.tostring(ipEntry.AddressList[2]);

        return ipaddress.tostring();

    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900