locked
How to get hostname from ip adress provided? RRS feed

  • Question

  • Hi All,

    I have IP address of machine and need to get hostname from it through API. Can we get host name from IP address through networking API? Please help on the same. 

    Thanks,

    Nandu.


    Nandkumar T.

    Monday, July 2, 2012 6:05 AM

Answers

  • you can use for  gethostname or WSAAsyncGetHostByName API for get the machine details. 

    The following link has the sample code 

    http://www.cplusplus.com/forum/articles/9742/


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    • Proposed as answer by Selvam Monday, July 2, 2012 2:17 PM
    • Marked as answer by Helen Zhao Monday, July 9, 2012 4:03 AM
    Monday, July 2, 2012 2:17 PM
  • Bruno, gethostbyname requires the host name, not the IP address...

    Oops. I think I meant gethostbyaddr. I was confused by the fact that gethostbyname can also take an IP address. I know I used them both in a unix console app. A quick trip to msdn learns that gethostbyaddr is deprectead though. Msdn advises to use getnameinfo instead

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms738532(v=vs.85).aspx

    • Marked as answer by Helen Zhao Monday, July 9, 2012 4:04 AM
    Monday, July 2, 2012 3:53 PM

All replies

  • Without testing but while using my good friend google: http://www.dbhagen.com/?page_id=30
    Monday, July 2, 2012 6:27 AM
  • I've done this using the gethostbyname API. This is a C style function.

    For .NET code there are probably more user friendly methods. For unmanaged code the above API works well.

    Monday, July 2, 2012 1:37 PM
  • Bruno, gethostbyname requires the host name, not the IP address...
    Monday, July 2, 2012 2:15 PM
  • you can use for  gethostname or WSAAsyncGetHostByName API for get the machine details. 

    The following link has the sample code 

    http://www.cplusplus.com/forum/articles/9742/


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    • Proposed as answer by Selvam Monday, July 2, 2012 2:17 PM
    • Marked as answer by Helen Zhao Monday, July 9, 2012 4:03 AM
    Monday, July 2, 2012 2:17 PM
  • Bruno, gethostbyname requires the host name, not the IP address...

    Oops. I think I meant gethostbyaddr. I was confused by the fact that gethostbyname can also take an IP address. I know I used them both in a unix console app. A quick trip to msdn learns that gethostbyaddr is deprectead though. Msdn advises to use getnameinfo instead

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms738532(v=vs.85).aspx

    • Marked as answer by Helen Zhao Monday, July 9, 2012 4:04 AM
    Monday, July 2, 2012 3:53 PM