Thread: gethostbyname()

  1. #1
    Registered User
    Join Date
    Nov 2001


    Please advise why the following code is not working(Segmentation fault). Merry Chirstmas!

    #include <stdio.h>
    #include <netdb.h>

    main() {

    struct hostent *h;
    char *output1;
    char *input;

    fgets(input, strlen(input), stdin);
    printf("%s", input);
    h = gethostbyname(input);
    sprintf(output1, "%u.%u.%u.%u", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned cha
    printf("%s", output1);

  2. #2
    Registered User
    Join Date
    Oct 2001
    >char *input;

    >fgets(input, strlen(input), stdin);
    // input is a char pointer,and u should allocate memory for it first

    char input[1024];


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gethostbyname() won't work...
    By headbr in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-14-2008, 06:51 AM
  2. GetHostByName
    By maxorator in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2005, 02:08 PM
  3. gethostbyname allways NULL
    By BianConiglio in forum Windows Programming
    Replies: 6
    Last Post: 08-18-2005, 01:27 AM
  4. Replies: 1
    Last Post: 09-11-2004, 08:52 AM
  5. WinSock and gethostbyname() won't return correctly...
    By SyntaxBubble in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2002, 12:08 PM