Thread: Hostname to IP Address for Fx bookmarks

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The gethostbyname() call is deprecated only because it doesn't work with unicode.

    If you're not using unicode... no problem, and it's about 3 times faster than the newer versions.

    It's not like it's going to stop working or be removed, it will be around for a long time to accomodate older programs. I'd use it anyway, especially in your project where you have to process large numbers of of URLs.

  2. #17
    Registered User htoip's Avatar
    Join Date
    Dec 2010
    Posts
    9
    OK. It makes the code more concise, too, and now the Windows and GNU/Linux code is much closer.

    Code:
    char *getip(char *s, char *u, int u_len)
    
    {
    
    #ifdef _WIN32_
    
      HOSTENT *hostentptr;
    
      IN_ADDR addr; /* IP address */
    
    #endif /* _WIN32_ */
    
    #ifdef _LINUX_
    
      struct hostent *hostentptr;
    
      struct in_addr *addr;/* IP address */
    
    #endif /* _LINUX_ */
    
        /* get HOSTENT data */
    
        hostentptr = gethostbyname(s);
    
        if ( hostentptr == NULL )
    
        {   fprintf(stderr, "getip: %s: Unresolvable hostname; returning hostname\n", s);
    
            strncpy(u, s, u_len);
    
            return(u);
    
        }
    
        /* extract main IP address */
    
    #ifdef _WIN32_
    
        addr.S_un.S_addr = *(ULONG *)hostentptr->h_addr_list[0];
    
    #endif /* _WIN32_ */
    
    #ifdef _LINUX_
    
        addr = (struct in_addr *)hostentptr->h_addr_list[0];
    
    #endif /* _LINUX_ */
    
        strncpy(u, inet_ntoa(*addr), u_len);
    
        return(u);
    
    }

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You can probably get it even more concise than that...

    both in_addr and hostent structs exist as lower case in Win32... so you can probably merge the first two ifdef section without problems.

    I also suspect you can also do the same with the last section since the whole addr.S_un.S_addr thing probably also exists on Linux... But that would have to be tested.

  4. #19
    Registered User htoip's Avatar
    Join Date
    Dec 2010
    Posts
    9
    I didn't think it would be this simple. Now there's no platform-specific code in getip(). You've been a big help, CommonTater.
    Here it is now.
    Code:
    char *getip(char *s, char *u, int u_len)
    { struct hostent *hostentptr;
      struct in_addr *addr; /* IP address */
        /* get HOSTENT data */
        hostentptr = gethostbyname(s);
        if ( hostentptr == NULL )
        {   fprintf(stderr, "getip: %s: Unresolvable hostname; returning hostname\n", s);
            strncpy(u, s, u_len);
            return(u);
        }
        /* extract main IP address */
        addr = (struct in_addr *)hostentptr->h_addr_list[0];
        strncpy(u, inet_ntoa(*addr), u_len);
        return(u);
    }
    htoip source

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by htoip View Post
    I didn't think it would be this simple. Now there's no platform-specific code in getip(). You've been a big help, CommonTater.
    No worries... The nice thing about sockets and winsock is they're both based on the Berkley standard for TCP/IP so you end up with a lot of very useful similarities.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strstr problem
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 09-08-2009, 03:46 AM
  2. how to get hostname and IP address of a machine
    By Chandana in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-17-2009, 11:56 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM