C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 03-26-2006, 09:40 PM   #1
Intranasal Heroin User
 
Xterria's Avatar
 
Join Date: Sep 2001
Location: Buffalo, NY
Posts: 1,033
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
Xterria is offline   Reply With Quote
Old 03-26-2006, 09:44 PM   #2
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
Probably because it returns a pointer to a HOSTENT stucture. On msdn it says:
Quote:
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:
Quote:
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
JaWiB is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:58 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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