0

My below code is getting ivp6 ip address am not sure how to get ipv4 in the same manner.

 string ipAddress = "";
           if (Dns.GetHostAddresses(Dns.GetHostName()).Length > 0)
           {
               ipAddress = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
           }
2
  • 2
    Dns.GetHostName() has nothing to do with the client. Commented Dec 21, 2015 at 15:57
  • string strHostName = System.Net.Dns.GetHostName(); IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName); IPAddress[] addr = ipEntry.AddressList; In IPAddress array you can will get both the IP'S IPV4 and IPV6 Commented Dec 23, 2015 at 12:26

1 Answer 1

2

This code: Dns.GetHostAddresses(Dns.GetHostName())

Will return an array of all IP addresses assigned to the local machine. When you assign to the ipAddress variable, you are using "[0]", which only takes the first IP address in the list.

Look at the entire array and you might find that you are indeed getting the IPv4 addresses too.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for sharing ur knowledge but how to do that ma not sure
Take at look at the example in the documentation for GetHostAddresses. It shows how to loop through the results.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.