1

I want to get the adrress IP (LAN ip) of the computer which access my site.

How can i get it?

Sampath
  • 63,341
  • 64
  • 307
  • 441
BrMe
  • 315
  • 2
  • 8
  • 22
  • possible duplicate of [How to get a user's client IP address in ASP.NET?](http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net) – Tomasz Wojtkowiak Dec 16 '12 at 15:46

4 Answers4

1

You can't.

Browsers don't send their local IPs in HTTP headers, so there is no way for you to get it. You only get the router's external (internet) IP.

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96
  • I can get it...see the answer I post – BrMe Dec 16 '12 at 12:18
  • 2
    No. You can get it in a local program, of course. But you can't get people's local ip addresses who connect to your website through HTTP. In your answer, it only works on local computers. – Umur Kontacı Dec 16 '12 at 14:39
  • ah,you right..are you sure that there isnt any way to get it? – BrMe Dec 17 '12 at 10:36
  • I'm absolutely sure. For the record, browsers don't send their remote addresses by themselves either, its is your server who gathers the internet ip of the client from TCP connection. And when the data is sent back to the client through internet IP, it is client's router's job to send it to the local computer in client's network. So, no. – Umur Kontacı Dec 17 '12 at 12:31
  • ok!thank for the explanations!someone told me that I can do it in client side...do you know something about it? – BrMe Dec 17 '12 at 12:51
  • In every desktop application (executable files), you can get the local network ip; but since you are using a web application, you are bound to client's browser and javascript which prevents you doing so as a security measure. – Umur Kontacı Dec 17 '12 at 13:02
  • so i realy stuck,ah? do you have another suggestion for me? my goal is:I have a machine which scan diamonds. and displays results in the site. for now,when i enter the site i can see all machines which connected to site.but I want that the client will be able to see just the machines which connected in his network. – BrMe Dec 17 '12 at 13:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21270/discussion-between-brme-and-fastreload) – BrMe Dec 17 '12 at 13:13
0

I assume you're using asp.net and in that case you could use Request.UserHostAddress to retrieve the IP address of a client.

johan
  • 6,578
  • 6
  • 46
  • 68
  • I tried this.but I got the internetIp.I want to get the intranetIP – BrMe Dec 16 '12 at 10:43
  • Ok. I see. Why do you want that? – johan Dec 16 '12 at 10:46
  • I a page in my site that display all the machines whice connected to the site.I want to display just the machines which in my network(internal network) – BrMe Dec 16 '12 at 10:50
  • But if you get internal IPs it's risk that they are duplicates. How would you determine if it's really from your network? – johan Dec 16 '12 at 10:51
  • At first,I get the internet IP of the machine and the client,if its the same i check the intranetIP. – BrMe Dec 16 '12 at 10:55
  • My next step is to get the subnet mask by the IP,and compare the IPs – BrMe Dec 16 '12 at 10:57
0

Method 1:

You can get that by using below mentioned link.

List the IP Address of all computers connected to a single LAN

Method 2:

You can try below one also.

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

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

        return Request.ServerVariables["REMOTE_ADDR"];
    }

Method 3:

System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();

I hope this will help to you.

Community
  • 1
  • 1
Sampath
  • 63,341
  • 64
  • 307
  • 441
0

Use this function:

 public string GetLanIP()
    {
        IPHostEntry host;
        string localIP = "?";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                localIP = ip.ToString();
            }
        }
        return localIP;

    }
BrMe
  • 315
  • 2
  • 8
  • 22