Created
June 19, 2021 15:34
-
-
Save keddad/2fe20124d8ca7f4706820a25cbaf2f3a 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 <cstdio> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <arpa/inet.h> | |
int main() { | |
struct addrinfo *res = nullptr; | |
getaddrinfo("google.com", "443", nullptr, &res); | |
struct addrinfo *i; | |
for (i = res; i != nullptr; i = i->ai_next) { | |
char str[INET6_ADDRSTRLEN]; | |
if (i->ai_addr->sa_family == AF_INET) { | |
auto *p = (struct sockaddr_in *) i->ai_addr; | |
printf("%s\n", inet_ntop(AF_INET, &p->sin_addr, str, sizeof(str))); | |
} else if (i->ai_addr->sa_family == AF_INET6) { | |
auto *p = (struct sockaddr_in6 *) i->ai_addr; | |
printf("%s\n", inet_ntop(AF_INET6, &p->sin6_addr, str, sizeof(str))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment