none
why dns.gethotentry() method return addresslist as empty? RRS feed

  • Question

  • I use the following code to get the IP address:

     
    var ip =   Dns.GetHostEntry(host);                   
     var ipaddress =ip.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString();

    it return the hostname correctly. but the Addresslist is empty or does not return ipv4 address.

    i can able to ping this hostname. and also it does not working, while i am giving local machine ipaddress.

    Tuesday, July 28, 2015 6:33 AM

Answers

  • Return type of AddressList property is 
    IPAddress[] but not string[] , if you use IntelliTrace for seen local value maybe you see  
    Aliases property returned value that is string[]
    Tuesday, July 28, 2015 1:00 PM
  • Hi 

    sorry you wrongly understand my question.

    for example

    my machine ip=74.125.232.116

    i pass this ip to dns.GetHostEntry("74.125.232.116")

    it return hostname="selva"

    but ipaddresslist is empty (string{0}). so i cant get my machine ip. 

    Thanks.

    Hi selva,

    I've tested your code,

      IPHostEntry hostEntry;
    
                hostEntry = Dns.GetHostEntry("10.168.XX.XX");

    Here is my test result. AddressList property returns IPAddress[2] and Aliases returns string[0]. As Azat.Tazayan said,

    Return type of AddressList property is  IPAddress[] but not string[], please check again, I suspect you misunderstood something.
    IPAddress[]


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, July 29, 2015 8:38 AM

All replies

  • Hello,

    For get your local machine IPv4 address passed an empty string as the host name

    static void Main(string[] args)
            {
                var ip = Dns.GetHostEntry(string.Empty);
                var ipaddress = ip.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().ToString();
                Console.WriteLine(ipaddress);
                Console.ReadKey();
            }

    and for other hosts for example google

    static void Main(string[] args)
            {
                var ip = Dns.GetHostEntry("www.google.com");
                var ipaddress = ip.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().ToString();
               //one of ipaddresses = 74.125.232.116
                Console.WriteLine(ipaddress);
            }

     If the answer helped you, don't forget mark it as answer.. Thanks !
    • Edited by Azat Tazayan Tuesday, July 28, 2015 8:03 AM
    • Proposed as answer by Azat Tazayan Wednesday, July 29, 2015 8:53 AM
    • Unproposed as answer by Azat Tazayan Wednesday, July 29, 2015 8:54 AM
    Tuesday, July 28, 2015 8:03 AM
  • Hi 

    sorry you wrongly understand my question.

    for example

    my machine ip=74.125.232.116

    i pass this ip to dns.GetHostEntry("74.125.232.116")

    it return hostname="selva"

    but ipaddresslist is empty (string{0}). so i cant get my machine ip. 

    Thanks.



    Tuesday, July 28, 2015 11:20 AM
  • Return type of AddressList property is 
    IPAddress[] but not string[] , if you use IntelliTrace for seen local value maybe you see  
    Aliases property returned value that is string[]
    Tuesday, July 28, 2015 1:00 PM
  • Hi 

    sorry you wrongly understand my question.

    for example

    my machine ip=74.125.232.116

    i pass this ip to dns.GetHostEntry("74.125.232.116")

    it return hostname="selva"

    but ipaddresslist is empty (string{0}). so i cant get my machine ip. 

    Thanks.

    Hi selva,

    I've tested your code,

      IPHostEntry hostEntry;
    
                hostEntry = Dns.GetHostEntry("10.168.XX.XX");

    Here is my test result. AddressList property returns IPAddress[2] and Aliases returns string[0]. As Azat.Tazayan said,

    Return type of AddressList property is  IPAddress[] but not string[], please check again, I suspect you misunderstood something.
    IPAddress[]


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, July 29, 2015 8:38 AM