Last active
November 25, 2019 21:27
-
-
Save ChongyeWang/04adb0bdce84ba716fa97ff6c1fc48f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
struct addrinfo hints, *infoptr; // So no need to use memset global variables | |
int main() { | |
hints.ai_family = AF_INET; // AF_INET means IPv4 only addresses | |
int result = getaddrinfo("www.bbc.com", NULL, &hints, &infoptr); | |
if (result) { | |
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(result)); | |
exit(1); | |
} | |
struct addrinfo *p; | |
char host[256]; | |
for (p = infoptr; p != NULL; p = p->ai_next) { | |
getnameinfo(p->ai_addr, p->ai_addrlen, host, sizeof (host), NULL, 0, NI_NUMERICHOST); | |
puts(host); | |
} | |
freeaddrinfo(infoptr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment