本人比来getaddrinfo函数获取本机IP地址,本机的IPV4地址能正确获得,但本机IPV6地址只能获得环路地址,即::1。为什么不克不及获得IPV6的Global地址或链路当地地址。恳请指点,不堪感恩!源代码如下:
char AddrName[128];
int RetVal;
BOOL RunForever = FALSE;
WSADATA wsaData;
struct addrinfo Hints, *AddrInfo, *AI;
CString str;
if ((RetVal = WSAStartup(MAKEWORD(2, 2), wsaData)) != 0)
str.Format(“WSAStartup failed with error %d\n“,RetVal);
m_listbox.AddString( str );
WSACleanup();
return ;
char szhostname[128];
memset(szhostname,0,128);
memset(Hints, 0, sizeof(Hints));
Hints.ai_family = AF_UNSPEC;
// Hints.ai_family = AF_INET6;
Hints.ai_socktype = SOCK_STREAM;
Hints.ai_flags=AI_CANONNAME;
gethostname(szhostname,sizeof(szhostname));
RetVal = getaddrinfo(static_castLPCTSTR(szhostname),0, Hints, AddrInfo);
if (RetVal != 0)
str.Format(“不克不及解析:主机[%s],错误 %d: %s\n“,szhostname, RetVal, gai_strerror(RetVal));
m_listbox.AddString( str );
WSACleanup();
return;
//将IP 地址转换成数字式字符文本
for (AI=AddrInfo; AI!= NULL;AI=AI-ai_next)
if (getnameinfo(AI-ai_addr,AI-ai_addrlen,AddrName,sizeof(AddrName),NULL,0,NI_NUMERICHOST) != 0)
strcpy(AddrName, “unknown“);
str.Format(“IPv%d: %s\n“,(AI-ai_family==AF_INET6?6:4), AddrName);
m_listbox.AddString( str );
WSACleanup();