Game Development Reference
In-Depth Information
The first value, 0x02104080, is the unsigned long that is the converted IP address for
128.64.16.2. This is already in network order, so you can use it in socket functions
without converting it. The next value, 0x18288488, shows you what happens when
you send 0x88482818 through the htonl() function on my Dell. Your mileage
may vary if you happen to use a non-Intel
based machine! The last string on the
output line is 136.72.40.24, which is the dotted decimal format for the IP address
given by htonl(0x88482818) .
This can be devilishly confusing, so choose a nice calm day to start playing with net-
work programming.
-
Domain Name Service (DNS) Functions
The next set of functions helps you make use of DNS:
Retrieves host information, such as IP
address, from a dotted-decimal format
string, such as
struct hostent* FAR gethostbyname(
const char* name
www.yahoo.com.
If the
);
host doesn
'
t exist, you
'
ll get back NULL.
Retrieves host information, such as IP
address, from an in_addr structure for
IPv4 or in6_addr structure for IPv6. If the
host doesn
struct hostent* FAR gethostbyaddr(
const char* addr,
int len,
int type
'
t exist, you
'
ll get back NULL.
);
Both of these functions look up host information based on an address, either a text
string in dotted-decimal notation or an IP address in network order. Don ' t let the
const char * fool you in gethostbyaddr() because it doesn
'
t want a text string.
Here
'
s a quick example of using both of these:
const char *host =
;
struct hostent *pHostEnt = gethostbyname(host);
ftp.microsoft.com
if (pHostEnt == NULL)
fprintf(stderr,
No such host
);
else
{
struct sockaddr_in addr;
memcpy(&addr.sin_addr,pHostEnt->h_addr,pHostEnt->h_length);
printf(
Address of %s is 0x%08x\n
, host, ntohl(addr.sin_addr.s_addr));
}
 
 
Search WWH ::




Custom Search