Results 1 to 5 of 5

Thread: Just to be sure, get remote IP address

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,594

    Just to be sure, get remote IP address

    Hi.
    I'm asking to be sure.
    I do this to get a PC address on forms:
    Code:
     public static String GetLocalIPAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if(ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                    return ip.ToString();
                }
            }
    
            return "";
        }
    Now this will not work on internet get's but I've read that it will work with Local networks. Is that the case? Meaning I can get an address of a PC that is on the network using a web application or I will just get the server IIS name?

    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,447

    Re: Just to be sure, get remote IP address

    Quote Originally Posted by sapator View Post
    Hi.
    I'm asking to be sure.
    I do this to get a PC address on forms:
    Code:
     public static String GetLocalIPAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if(ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                    return ip.ToString();
                }
            }
    
            return "";
        }
    Now this will not work on internet get's but I've read that it will work with Local networks. Is that the case? Meaning I can get an address of a PC that is on the network using a web application or I will just get the server IIS name?

    Thanks.
    Dns.GetHostName will return whatever IPAddresses are assigned to the local machine, these addresses are normally local to the network and not directly reachable over the internet. Your code will actually only return the 1st address associated with the local machine, rather than all of them the following might be better as it returns all of them
    Code:
    static IEnumerable<String> GetLocalIPAddress()
    {
         var host = Dns.GetHostEntry(Dns.GetHostName());
         foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
            yield return ip.ToString();
            }
        }
    }
    If you are trying to get the external address of you local machine you might be better making an http request to something like http://checkip.dyndns.org/ as this will report on your public address (typically your public router or firewall). Anything on the internet could then reach that, although there is a good chance you would then need to configure the public firewall / router / whatever to allow connections in to your actual pc.

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,594

    Re: Just to be sure, get remote IP address

    Hey.
    I'm actually trying to get whatever machine local address the PC is using. So mine is 192.168.1.1 when a person on another building on the local network hits the site it will be 192.168.20.1 etc.
    So simulate a whatismyip thing. I'm thinking it might be able to be done with javascript but from what I've read the browsers might or might not support it.

    From what I'm reading I believe something more viable would be this: https://www.c-sharpcorner.com/blogs/...ress-in-mvc-30
    Will have to try it out tho and since next week I'm working from home, err, it will take a while.


    Edit: Tested it locally and it returns ::1 , so I don't have a clue on what will return remotely.
    Last edited by sapator; Oct 30th, 2021 at 12:18 PM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Just to be sure, get remote IP address

    This will return the server ip address where the application is running not the client IP address. You can try something like

    Code:
    string clientIp = (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? 
                       Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();
    This will get the details from browser. Also you can try printing the values of Request.ServerVariables and see if that has more useful values
    Please mark you thread resolved using the Thread Tools as shown

  5. #5
    New Member
    Join Date
    Mar 2020
    Location
    India
    Posts
    3

    Re: Just to be sure, get remote IP address

    This should help

    Code:
    private string GetUserIP()
        {
            string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    
            if (!string.IsNullOrEmpty(ipList))
            {
                return ipList.Split(',')[0];
            }
    
            return Request.ServerVariables["REMOTE_ADDR"];
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width