Find the answer to your Linux question:
Results 1 to 3 of 3
hii.. I am new to unix network programming.I have a trouble in running the code for fetching the ip address of host. Can you please help me out in getting ...
  1. #1
    Linux Newbie
    Join Date
    Nov 2005
    Posts
    172

    Need help in running gethostbyname

    hii.. I am new to unix network programming.I have a trouble in running the code for fetching the ip address of host. Can you please help me out in getting it done. The following is the code

    #include <stdio.h>
    #include <netdb.h>
    #include <netinet/in.h>

    struct hostent *he;
    struct in_addr a;

    int
    main (int argc, char **argv)
    {
    if (argc != 2)
    {
    fprintf(stderr, "usage: %s hostname\n", argv[0]);
    return 1;
    }
    he = gethostbyname (argv[1]);
    if (he)
    {
    printf("name: %s\n", he->h_name);
    while (*he->h_aliases)
    printf("alias: %s\n", *he->h_aliases++);
    while (*he->h_addr_list)
    {
    bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
    printf("address: %s\n", inet_ntoa(a));
    }
    }
    else
    herror(argv[0]);
    return 0;
    }

    ->Please correct me if the program is wrong....
    -> the compilation was fine. I am unable to get the output if i give the hostname.
    Can anyone help me ?

  2. #2
    Linux Newbie
    Join Date
    Nov 2005
    Posts
    172
    I am getting segmentation fault...
    please help...
    thanks in advance

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695

Posting Permissions

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