Resolving hostname into IP

This is a discussion on Resolving hostname into IP within the Networking/Device Communication forums, part of the General Programming Boards category; Found an example by Beej but I can't seem to get it working: Code: #include <stdio.h> #include <winsock.h> int main(int ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Resolving hostname into IP

    Found an example by Beej but I can't seem to get it working:

    Code:
    #include <stdio.h>
    #include <winsock.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        struct hostent *he;
        struct in_addr **addr_list;
    
        if (argc != 2) {
            fprintf(stderr,"usage: ghbn hostname\n");
            return 1;
        }
    
        if ((he = gethostbyname(argv[1])) == NULL) {  // get the host info
            herror("gethostbyname");
            return 2;
        }
    
        // print information about this host:
        printf("Official name is: %s\n", he->h_name);
        printf("    IP addresses: ");
        addr_list = (struct in_addr **)he->h_addr_list;
        for(i = 0; addr_list[i] != NULL; i++) {
            printf("%s ", inet_ntoa(*addr_list[i]));
        }
        printf("\n");
    
        return 0;
    }
    Error:

    error C3861: 'herror': identifier not found

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Did you read the "beej" paragraph on "What to do if you're stuck with windoiws"?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Then herror() doesn't exist. You don't define it thus it has to be from the headers you include. Check the header it needs and if you are including it (google/bing/yahoo/ask are all your good buddies)

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    It's from netdb.h which doesn't work on Windows. Instead of herror I tried perror but this brought about the following linker errors:

    1>main.obj : error LNK2019: unresolved external symbol _inet_ntoa@4 referenced in function _main
    1>main.obj : error LNK2019: unresolved external symbol _gethostbyname@4 referenced in function _main

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    You also need to link in the winsock lib file.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by rags_to_riches View Post
    You also need to link in the winsock lib file.
    Sorry, I should've mentioned that I'd already done that.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by david84 View Post
    Code:
        if ((he = gethostbyname(argv[1])) == NULL) {  // get the host info
            herror("gethostbyname");
            return 2;
        }
    
      }
    Error:

    error C3861: 'herror': identifier not found
    Judging from the position in the code you could simply substitute something like

    Code:
        he = gethostbyname(argv[1];
        if (!he) 
          { puts("Host not found");
            return 2; }
    and still have it working fine.

  8. #8
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Quote Originally Posted by david84 View Post
    Sorry, I should've mentioned that I'd already done that.
    With that kind of error it means you did not. Are you compiling this via command line or are you using an IDE. If its command line, try adding this switch: -lws2_32

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by carrotcake1029 View Post
    With that kind of error it means you did not. Are you compiling this via command line or are you using an IDE. If its command line, try adding this switch: -lws2_32
    Yes, you are correct. I must've got mixed up trying different ones.
    However, I'm still having some issues:
    It hits the code CommonTater suggested and exists. Is it just my system?
    Code:
    #pragma comment(lib, "ws2_32")
    #include <stdio.h>
    #include <winsock.h>
    
    int main(int argc, char *argv[])
    {
        int i;
        struct hostent *he;
        struct in_addr **addr_list;
    
        if (argc != 2) {
            fprintf(stderr,"usage: ghbn hostname\n");
    		//getchar();
            return 1; //exits here
        }
    
    	he = gethostbyname(argv[1]);
        if (!he) {
    		puts("Host not found");
            return 2;
    	}
    
        // print information about this host:
        printf("Official name is: %s\n", he->h_name);
        printf("    IP addresses: ");
        addr_list = (struct in_addr **)he->h_addr_list;
        for(i = 0; addr_list[i] != NULL; i++) {
            printf("%s ", inet_ntoa(*addr_list[i]));
        }
        printf("\n");
        return 0;
    }

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You may have to mod my suggestion to say if (he == NULL) ... Sometimes the NOT test (!) doesn't catch everything it should.

    The big reason it would hit "my code" and exit is becaus the host lookup failed.

    If you call wsagetlasterror immediately after detecting the null he pointer, you can find out what went wrong...

    Also remember that http://www.munch.com/user is not a host name... munch.com is.

    For testing try looking up some of the computer names on your LAN... keep it simple before you go after the big stuff.
    Last edited by CommonTater; 09-13-2010 at 06:12 PM. Reason: Afterthought

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by CommonTater View Post
    You may have to mod my suggestion to say if (he == NULL) ... Sometimes the NOT test (!) doesn't catch everything it should.

    The big reason it would hit "my code" and exit is becaus the host lookup failed.

    If you call wsagetlasterror immediately after detecting the null he pointer, you can find out what went wrong...

    Also remember that munch.com: The Leading Food Site on the Net is not a host name... munch.com is.

    For testing try looking up some of the computer names on your LAN... keep it simple before you go after the big stuff.
    Thanks for all your help... I did return WSAGetLastError(); which gave me an error code which indicated I failed to call WSAStartup.. I had that in a previous version but I kept messing around with different ones. Thanks again

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. resolving names and ip addresses
    By dicky in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-01-2004, 03:32 PM
  3. Resolving Hostname :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-08-2002, 08:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21