| Description | The gethostbyname function retrieves host information corresponding to a host name from a host database. The argument name is a pointer to the null-terminated name of the host to resolve. The argument err is a pointer to the return error code. The gethostbyname function is in the RL-TCPnet library. The prototype is defined in rtl.h. note - If a negative number is returned, it represents an error code.
|
| Example |
#include <rtl.h>
static const char *hosts[] = {
"www.google.com",
"www.keil.com",
"www.microsoft.com",
"www.yahoo.com",
NULL
};
__task void dns_task (void) {
int i,j,err;
HOSTENT *host;
IN_ADDR *addr;
for (i = 0; ; i++) {
if (!hosts[i]) i = 0;
printf ("Host: %s\n",hosts[i]);
host = gethostbyname (hosts[i], &err);
if (host == NULL) {
if (err == SCK_ENONAME) {
printf ("Hostname does not exist\n");
}
}
else if (host->h_addrtype == AF_INET) {
for (j = 0; host->h_addr_list[j]; j++) {
addr = (IN_ADDR *)host->h_addr_list[j];
printf("IP Address: %d.%d.%d.%d\n",addr->s_b1,
addr->s_b2,
addr->s_b3,
addr->s_b4);
}
}
os_dly_wait (300);
}
}
|