Added 10 more gists.
This commit is contained in:
1
hosts.c/README.md
Normal file
1
hosts.c/README.md
Normal file
@@ -0,0 +1 @@
|
||||
A simple test of gethostbyname()
|
40
hosts.c/hosts.c
Normal file
40
hosts.c/hosts.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
typedef struct in_addr addr_t;
|
||||
|
||||
int main(void) {
|
||||
int i;
|
||||
char * hostname = argc > 1 ? argv[1] : "google.com";
|
||||
struct hostent * host = gethostbyname(hostname);
|
||||
|
||||
if (host == NULL) {
|
||||
herror(argv[0]);
|
||||
return 1;
|
||||
} else {
|
||||
|
||||
/* Print standard host name. */
|
||||
printf("NAME: %s\n", host->h_name);
|
||||
|
||||
/* Print all host aliases if any. */
|
||||
for (i = 0; host->h_aliases[i]; i++)
|
||||
printf("ALIAS %d: %s\n", i, host->h_aliases[i]);
|
||||
|
||||
/* Print if addresses are IPv4 or IPv6. */
|
||||
printf("TYPE: %s\n", host->h_addrtype == AF_INET ? "IPv4" : "IPv6");
|
||||
|
||||
/* Print address length in bytes. */
|
||||
printf("LENGTH: %d\n", host->h_length);
|
||||
|
||||
/* Print all addresses. */
|
||||
for (i = 0; host->h_addr_list[i]; i++) {
|
||||
addr_t * addr = (addr_t *)host->h_addr_list[i];
|
||||
printf("ADDR: %s\n", inet_ntoa(*addr));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user