Thread: gethostbyname

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    7

    Post gethostbyname

    Code:
    //begin code section 1
    h_test = gethostbyname(TEST);
    if(h_test==NULL) {
    exit(1);
    }    
    //end code section 1
    tbox_addr.sin_family = h_test->h_addrtype;    
    memcpy((char *) &tbox_addr.sin_addr.s_addr, h_test->h_addr_list[0], h_test->h_length);    
    tbox_addr.sin_port = htons(TEST_PORT);
    
    result = gethostname(BOX, 64);
    if(result != 0){
        exit(1);
    }
    
    //begin code section 2
    h_ip = gethostbyname(BOX);
    if(h_ip==NULL) {
        exit(1);
    }  
    //end code section 2
    box_addr.sin_family = h_ip->h_addrtype;    
    memcpy((char *) &box_addr.sin_addr.s_addr, h_ip->h_addr_list[0], h_ip->h_length);    
    box_addr.sin_port = htons(BOX_PORT);
    Hello,
    Can someone explain to me why it is that if I switch the order of code section 1 and code section 2 then the value at box_addr.sin_addr.s_addr is actually h_test->h_addr_list[0] rather than h_ip->h_addr_list[0]? Is a static variable being used or something? Also, do I need to free the value returned by gethostbyname?

    Thanks,

    shawn
    Last edited by shawn_drn; 02-13-2005 at 12:40 PM.

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    7
    To answer my own question, yes the storage is static. No, do not free it.

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