I use getaddrinfo(3) to get list ip address from a domain which support both ipv4 and ipv6, but the local system support ipv4 only or both ipv4 & ipv6, As I expect, if system support both ipv4 & ipv6 getaddrinfo(3) will query both ipv4 & ipv6 and if system support ipv4 only, getaddrinfo(3) will query only ipv4. I try to set ai_flags to AI_ADDRCONFIG but it still query both ipv4 & ipv6 while system support ipv4 only. How should I do

Here is my code:

struct addrinfo hint, *res = NULL, *p = NULL;
int err;
ACE_OS::memset(&hint, 0, sizeof hint);
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
hint.ai_flags = AI_ADDRCONFIG;
err = ACE_OS::getaddrinfo(hostname.c_str() ,"http", &hint, &res);
  • Did you try "hint.ai_family = AF_INET;"? Jul 15, 2021 at 8:02

1 Answer 1


If you know you only want IPv4, set ai_family to AF_INET.

Use AF_INET for IPv4, AF_INET6 for IPv6.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .