Created
February 21, 2010 16:26
-
-
Save yinyin/310398 to your computer and use it in GitHub Desktop.
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 <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
int main(int argc, char ** argv) | |
{ | |
static struct addrinfo hint = { | |
.ai_flags = (0), | |
.ai_family = AF_INET, | |
.ai_socktype = SOCK_STREAM, | |
.ai_protocol = 0, | |
.ai_addrlen = 0, | |
.ai_addr = NULL, | |
.ai_canonname = NULL, | |
.ai_next = NULL | |
}; | |
while(1) { | |
struct addrinfo * result; | |
int ret; | |
result = NULL; | |
if(0 != (ret = getaddrinfo("localhost", NULL, &hint, &result))) | |
{ | |
fprintf(stderr, "ERR: Failed on hostname to address translation: %s. @(%s:%d)\n", gai_strerror(ret), __FILE__, __LINE__); | |
if(EAI_AGAIN == ret) | |
sleep(3); | |
else | |
break; | |
} | |
else | |
{ | |
if(NULL != result) | |
{ | |
printf("R: %d\n", result->ai_addrlen); | |
if(AF_INET == result->ai_addr->sa_family) | |
{ | |
u_int32_t addr_v4; | |
addr_v4 = ((struct sockaddr_in *)(result->ai_addr))->sin_addr.s_addr; | |
printf("AF_INET = 0x%X\n", addr_v4); | |
} | |
freeaddrinfo(result); | |
result = NULL; | |
} | |
else | |
{ | |
printf("R: NULL\n"); | |
} | |
break; | |
} | |
} | |
return 0; | |
} | |
/* | |
vim: ts=4 sw=4 ai nowrap | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment