Weird gethostbyname() issue

This is a discussion on Weird gethostbyname() issue within the Networking/Device Communication forums, part of the General Programming Boards category; Hey, I was just wondering why when gethostbyname()'s return value is assigned to a LPHOSTENT structure, using gethostbyname() one more ...

  1. #1
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035

    Weird gethostbyname() issue

    Hey, I was just wondering why when gethostbyname()'s return value is assigned to a LPHOSTENT structure, using gethostbyname() one more time reassigns that original variable, regardless of whether or not you told it to?

    For example,

    Code:
    LPHOSTENT lphost = NULL;
    LPHOSTENT lphost2 = NULL;
    
    //assign lphost to the IP at www.microsoft.com
    lphost = gethostbyname("www.microsoft.com");
    
    //assign a different var, lphost2, to www.cnn.com
    lphost2 = gethostbyname("www.cnn.com");
    
    //Assign to the address of lphost...
    client.sin_addr = *((LPIN_ADDR)*lphost->h_addr_list);
    One would think that client.sin_addr would point to the IP of microsoft.com. It doesn't. It points to cnn.com....which makes absolutely no sense at all. I've tested this several times and it's always the same. WHY? I'm using MSVC++ 6.

    Thanks

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Probably because it returns a pointer to a HOSTENT stucture. On msdn it says:
    The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.
    It also says:
    The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some weird issue.
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 04-12-2009, 05:10 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  3. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  4. weird pointer issue
    By Chaplin27 in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2006, 10:20 AM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 05:02 PM

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