Click here to Skip to main content
14,238,683 members
Rate this:
Please Sign up or sign in to vote.
See more:
i need to get user ipaddress....i use below code

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 12-Jan-18 2:40am
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....?
Rate this:
Please Sign up or sign in to vote.

Solution 3

Here is the solution to get all valid IP4 Address list
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

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();
}
   
Comments
Renju Vinod 14-Mar-13 3:22am
   
+5
King Fisher 22-Apr-14 9:02am
   
its perfect
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use
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;
  }
}
   
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
uspatel 7-Jun-12 2:38am
   
Perhaps,it is due to running on localhost?
Muthu Vinoth Kumar 7-Jun-12 2:44am
   
Error occured..
An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Request.get

How to rectify?
uspatel 7-Jun-12 2:49am
   
use this
this.Request.ServerVariables["REMOTE_ADDR"].ToString()
Muthu Vinoth Kumar 7-Jun-12 2:52am
   
CS0026: Keyword 'this' is not valid in a static property, static method, or static field initializer
uspatel 7-Jun-12 2:55am
   
make method non static and then use
uspatel 7-Jun-12 2:57am
   
check updated answer
Muthu Vinoth Kumar 7-Jun-12 3:14am
   
it returns 127.0.0.1
uspatel 7-Jun-12 3:15am
   
run it on iis,it returns right ip
Muthu Vinoth Kumar 7-Jun-12 3:29am
   
i need to run on online..whether it works?
uspatel 7-Jun-12 3:30am
   
Yes,It works.....
Muthu Vinoth Kumar 7-Jun-12 4:40am
   
thanks
Mas11 19-Mar-13 7:32am
   
Nice Code ! worked for me :)
Priyanka Sundaraj 26-Jun-14 8:03am
   
Thanks a Lot for this code
Rate this:
Please Sign up or sign in to vote.

Solution 2

hi,
try this, it will return your IP Address
string ip = "";
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();

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

IPAddress[] addr = ipEntry.AddressList;

ip = addr[2].ToString();
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 5

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();

    }
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100