CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    3

    function needed to convert hostname to ipaddress

    Hi ,
    I am writing a program for ping that should support both IPv4 and IPv6 .. i wrote the prog as such the dot ( . ) and colon ( is searched and variation is found out ...

    My question is " if a host name is given , how to convert it in to a ip address " .. Is there any function to convert it .. i am working on windows APIs and VC++ ...

    Thanks ,
    Chaitanya

  2. #2
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: function needed to convert hostname to ipaddress

    Hi sankris,

    try "getaddrinfo" ( WinSock 2 ).
    For details see the MSDN or ask again.

    Hope this helps,
    Matze

  3. #3
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: function needed to convert hostname to ipaddress

    gethostbyname()
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  4. #4
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: function needed to convert hostname to ipaddress

    Quote Originally Posted by NoHero
    gethostbyname()
    Sure, but Microsoft says in MSDN:

    Note The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

    So if you want to stay compatible use "getaddrinfo()".

    Greetings,
    Matze

  5. #5
    Join Date
    May 2005
    Posts
    4,954

    Re: function needed to convert hostname to ipaddress

    try this code:
    Code:
      WORD wVersionRequested;
      WSADATA wsaData;
      int err;
       wVersionRequested = MAKEWORD( 2, 2 );
      err = WSAStartup( wVersionRequested, &wsaData );
    
      char    Hostname[100];
      HOSTENT *pHostEnt;
      int  **ppaddr;
      SOCKADDR_IN sockAddr;
      CString addr;
    
      pHostEnt = gethostbyname( "www.codeguru.com");
      ppaddr = (int**)pHostEnt->h_addr_list;
      sockAddr.sin_addr.s_addr = **ppaddr;
      addr = inet_ntoa(sockAddr.sin_addr);  //this is your ip address 
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  6. #6
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: function needed to convert hostname to ipaddress

    Quote Originally Posted by matze42
    Sure, but Microsoft says in MSDN:

    Note The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

    So if you want to stay compatible use "getaddrinfo()".

    Greetings,
    Matze
    This deprecation is irrelevant because gethostbyname() has to work of course of BSD socket compatibility. Use what you want.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured