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();
           }
Shaik
  • 930
  • 1
  • 20
  • 48
  • 2
    `Dns.GetHostName()` has nothing to do with the client. – CodeCaster Dec 21 '15 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 – Ravi Kanth Dec 23 '15 at 12:26

1 Answers1

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.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • Thanks for sharing ur knowledge but how to do that ma not sure – Shaik Dec 22 '15 at 07:13
  • Take at look at the example in the documentation for [GetHostAddresses](https://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses(v=vs.110).aspx). It shows how to loop through the results. – Gabriel Luci Dec 22 '15 at 13:29