views:

37

answers:

2

It's from here,but fails when compiling:

int main(int argc, char **argv)
{
    struct hostent {
        char *h_name; // main name
        char **h_aliases; // alternative names (aliases)
        int h_addrtype; // address type (usually AF_INET)
        int h_length; // length of address (in octets)
        char **h_addr_list; // alternate addresses (in Network Byte Order)
    };
    #define h_addr h_addr_list[0] // First address of h_addr_list.


    struct hostent *info_stackoverflow;
    int i = 0;
    info_stackoverflow = gethostbyname( "www.stackoverflow.com" );
    printf("The IP address of %s is %s", 
           info_stackoverflow->h_name, 
           inet_ntoa( * ((struct in_addr *)info_stackoverflow->h_addr )));
    /* aliases */
    while( *(pc_ip->h_aliases + i) != NULL )
    {
        printf("\n\tAlias: %s", *(pc_ip->h_aliases + i) );
        i++;
    }
}
A: 

You need these three headers:

#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>

You should get rid of your own definition of struct hostent. It's already defined for you in netdb.h, and your definition will conflict.

Tip: try "man gethostbyname" on almost any Unix system; the manual page for most C functions will tell you what header files to include.

This still won't compile, because pc_ip is undefined. You're missing part of your code snippet, I guess.

dmazzoni
What libraries are also necessary?
httpinterpret
None, these functions are in the standard C library.
dmazzoni
A: 
#include <stdio.h>
#include <winsock.h>

Though struct hostent is already defined by Winsock, so you will want to remove the definition of hostent from your code snippet.

As dmazzoni has noted, pc_ip is not declared in that code. It is being used like a pointer to a hostent structure, so you could probably replace pc_ip with info_stackoverflow.

When you link, you will need to link against ws2_32.lib. At run-time, you'll probably have problems until you add a call to WSAStartup at the beginning of your code, and WSACleanup at the end, before you return from main.

Aaron Klotz
Can you figure out how's `pc_ip` defined?
httpinterpret
Oh,after replace pc_ip with info_stackoverflow,I got a error while linking: `error LNK2019: unresolved external symbol _inet_ntoa@4 referenced`
httpinterpret
It compiles OK now,but get an Access violation at this line `inet_ntoa( * ((struct in_addr *)info_stackoverflow->h_addr )));`
httpinterpret
You probably don't have a valid data structure; `gethostbyname` is probably failing, but your code isn't checking for failures. It is probably failing because you haven't added calls to `WSAStartup` and `WSACleanup`.
Aaron Klotz
I got a run-time error after adding WSAStartup and WSACleanup...
httpinterpret
That's not enough information. What's the error?
Aaron Klotz
It works for me. I just compiled it on Mac OS X and Linux with no problems.If you're still having trouble, please post more information (like what error you're getting) otherwise how can we help?
dmazzoni