Created
July 13, 2021 14:35
-
-
Save metalefty/4bf5804b0fe07aaec062e56f0bf53097 to your computer and use it in GitHub Desktop.
getaddrinfo.c
This file contains hidden or 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 <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
int main() | |
{ | |
char *hostname = "www.softether.com"; | |
struct addrinfo hints, *res; | |
struct in_addr addr; | |
int err; | |
char str[INET_ADDRSTRLEN]; | |
memset(&hints, 0, sizeof(hints)); | |
hints.ai_family = AF_UNSPEC; | |
hints.ai_flags = AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED; | |
if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) { | |
printf("error %s (%d)\n", gai_strerror(err), err); | |
return 1; | |
} | |
addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr; | |
inet_ntop(AF_INET, &addr.s_addr, str, INET_ADDRSTRLEN); | |
printf("ip address : %s\n", str); | |
freeaddrinfo(res); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment