Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get IP address of client machine

I am trying to get the IP address of client machine using C#. I am using the below code to get the IP address :

string IPAddress = HttpContext.Current.Request.UserHostAddress;

But it is giving me the response in encoded format i.e fe80::ed13:dee2:127e:1264%13

How can I get the actual IP address? Any one faced this issue please share some idea.

like image 644
Sushri Avatar asked Jan 16 '14 07:01

Sushri


People also ask

How do I find my client IP address in CMD?

First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.

What is the IP address of the client?

Client IP addresses describe only the computer being used, not the user. If multiple users share the same computer, they will be indistinguishable. Many Internet service providers dynamically assign IP addresses to users when they log in.

Which code will return the IP address of the client?

$_SERVER['REMOTE_ADDR'] contains the real IP address of the connecting party. That is the most reliable value you can find.

How to get the IP address of the client’s machine in PHP?

There are two ways to get the IP Address of the client’s machine in PHP. One way is to use the $_SERVER variable and another way is by using the getenv () function. $_SERVER Variable: It fetches the IP address of the machine from which the request was sent to the webserver. It is an array created by the Apache webserver.

What is a client IP?

There are two interpretations to this question. Most folks interpreted "Client IP" to mean the Public IP Address that Web server's see outside the LAN and out on the Internet. This is not the IP address of the client computer in most cases, though

How to find IP address of the client in C sharp?

C# program to find IP Address of the client Csharp Server Side Programming Programming Firstly find the hostname using the Dns.GetHostName () method in C# − String hostName = string.Empty; hostName = Dns.GetHostName (); Console.WriteLine ("Hostname: "+hostName);

How can we get the client's IP address in ASP NET MVC?

How can we get the client's IP address in ASP.NET MVC C#? How to get the ip address in C#? Firstly find the hostname using the Dns.GetHostName () method in C# − Now, use the IPHostEntry.AddressList Property to get IP Address −


4 Answers

C#

string IPAddress = GetIPAddress();

public string GetIPAddress()
{
    IPHostEntry Host = default(IPHostEntry);
    string Hostname = null;
    Hostname = System.Environment.MachineName;
    Host = Dns.GetHostEntry(Hostname);
    foreach (IPAddress IP in Host.AddressList) {
        if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
            IPAddress = Convert.ToString(IP);
        }
    }
    return IPAddress;
}

VB.net

Dim Host As IPHostEntry
Dim Hostname As String
Hostname = My.Computer.Name
Host = Dns.GetHostEntry(Hostname)
For Each IP As IPAddress In Host.AddressList
    If IP.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
        IPAddress = Convert.ToString(IP)
    End If
    Next
Return IPAddress

Hope this helps

like image 91
Amarnath Balasubramanian Avatar answered Sep 22 '22 20:09

Amarnath Balasubramanian


private string GetUserIP()
 {
     return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];    
 }

You may get several ip address, so can split them as-

private string GetUserIP()
    {
        string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipList))
        {
            return ipList.Split(',')[0];
        }

        return Request.ServerVariables["REMOTE_ADDR"];
    }
like image 28
Ramashankar Avatar answered Sep 22 '22 20:09

Ramashankar


In my project it's required to get the local PC IP. So i use it Please try the below code

string strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
string ip = addr[1].ToString();
like image 35
Sapnandu Avatar answered Sep 21 '22 20:09

Sapnandu


try using this

string ip=System.Net.Dns.GetHostEntry
               (System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
like image 28
The Hungry Dictator Avatar answered Sep 23 '22 20:09

The Hungry Dictator