none
How to determine ip address family from URL in .net 3.5 framework? RRS feed

  • Question

  • Dear Team,

    please help me to find out the address family from an url. The PC may have multiple NIC (IPv4 & IPv6)

    e.g: URL :

    1. http://localhost/test.aspx

    2. http://test-pc/test.aspx

    3. http://test-pc.domain.name.in/test.aspx

    4. http://168.25.36.25/test.aspx

    5. http://[2001:55:123:5]/test.aspx

    from the above url's i need to find the host is IPV4/IPv6?

    Thanks,

    Ravi


    • Edited by RaviRaju Friday, January 25, 2019 10:50 AM
    Friday, January 25, 2019 10:48 AM

Answers

  • Hi RaviRaju,

    Thank you for posting here.

    For your question, we could try the following code to determine whether the host is ipv4 or ipv6 from the url.

    static void Main(string[] args)
            {
                try
                {
                  var url = "http://localhost/test.aspx";
                    Uri myUri = new Uri(url);
                    var ip = Dns.GetHostAddresses(myUri.Host);
                    foreach (var item in ip)
                    {
                        IPAddress address;
                        if (IPAddress.TryParse(item.ToString(), out address))
                        {
                            switch (address.AddressFamily)
                            {
                                case System.Net.Sockets.AddressFamily.InterNetwork:
                                    Console.WriteLine(item + "  " + "it belongs to ipv4");
                                    break;
                                case System.Net.Sockets.AddressFamily.InterNetworkV6:
                                    Console.WriteLine(item + "   " + "it belongs to ipv6");
                                    break;
                                default:
                                    Console.WriteLine(item + "    " + "is incorrect");
                                    break;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
    
                    Console.WriteLine(e.ToString());
                }
               
            }
    

    Result:

    The ”::1” is the loopback address in IPv6., you could think of it as the IPv6 version of 127.0.0.1.

    Hope my advice could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 28, 2019 6:26 AM
    Moderator

All replies

  • The address family? A single DNS can have multiple address families. It will have one for each one it supports. Which one is used is dependent on which address family the caller is using. If you're on an IPv6 network then you'll use the IPv6, etc. DNS is ultimately just a mapping from the friendly name to an IP address (or series of IP addresses if you're in an advanced network configuration). Note that the domain is the only part that matters here. In your example you're putting pages of a Web Forms app. That has no impact on the IP address. 

    To get the set of IP addresses that your DNS server currently maps to a name use the Dns.GetHostAddresses method. This gives you back the IPs that map to this name. For each IPAddress returned use the AddressFamily property to determine whether it is v4 or v6. Remember a single DNS entry can have both.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, January 25, 2019 2:48 PM
    Moderator
  • Hi RaviRaju,

    Thank you for posting here.

    For your question, we could try the following code to determine whether the host is ipv4 or ipv6 from the url.

    static void Main(string[] args)
            {
                try
                {
                  var url = "http://localhost/test.aspx";
                    Uri myUri = new Uri(url);
                    var ip = Dns.GetHostAddresses(myUri.Host);
                    foreach (var item in ip)
                    {
                        IPAddress address;
                        if (IPAddress.TryParse(item.ToString(), out address))
                        {
                            switch (address.AddressFamily)
                            {
                                case System.Net.Sockets.AddressFamily.InterNetwork:
                                    Console.WriteLine(item + "  " + "it belongs to ipv4");
                                    break;
                                case System.Net.Sockets.AddressFamily.InterNetworkV6:
                                    Console.WriteLine(item + "   " + "it belongs to ipv6");
                                    break;
                                default:
                                    Console.WriteLine(item + "    " + "is incorrect");
                                    break;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
    
                    Console.WriteLine(e.ToString());
                }
               
            }
    

    Result:

    The ”::1” is the loopback address in IPv6., you could think of it as the IPv6 version of 127.0.0.1.

    Hope my advice could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 28, 2019 6:26 AM
    Moderator