Winsock: gethostbyname

Started by
5 comments, last by cbenoi1 19 years, 6 months ago
Hi I've recently decided to train me up in networking programming. Prior to this, I've never had any experience on it. I've been trying out some example tutorials from the net. One of them goes like the following:

WSAStartup(MAKEWORD(1,1), &wsaData);
LPHOSTENT lpHostEntry = gethostbyname("www.idgbooks.com");

Unfortunately lpHostEntry comes out NULL after the second line. Does anyone know why? What I ultimately would like to do is open up a socket to a web server and request pages from it via the http protocol.
Advertisement
I highly recommend Network Programming for Microsoft Windows, Second Edition by Anthony Jones and Jim Ohmund.

Check out MSDN.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/ntwrkprot.asp

Check out my website for software development links.

http://www.dslextreme.com/users/kuphryn/links.html

Kuphryn
Thanks, but can anyone help me without refering me to a book?
Try using "getaddrinfo" function, while gethostbyname was deprecated. It takes also IP address as parameter, so it helps a bit when confused, returns meaningful values and quite nice addrinfo struct.

Description of this function can be found on MSDN of course.

m.
I dont know moutch about network programming, but I belive you have to type the full address. http://www.idgbooks.com to find the hoste.

I can't connect to www.idgbooks.com, AFAIK if you use a site that don't exists the call whill return NULL
www.idgbooks.com without protocol (http://) SHOULD be resolved, while you don't need to know it to resolve domain name to IP address. The other thing, of course, is, if the address has DNS record somewhere or not... i did not checked it, but as you say, if that is the issue, i hope gethostbyname() will always return NULL and WSAGetLastError() will return something meaningful...

m.
I get redirected to another web site, which is a tell-tale sign.

gethostbyname() does a DNS lookup and will return both an MX and an A-type record if the host is found AND is reacheable. The function call will return a NULL and error code WSANO_DATA indicating the IP exist but cannot be reached directly.

-cb

This topic is closed to new replies.

Advertisement