急求在vc++6.0中获取IPV6地址的办法,高手请进,谢谢!!

3个月前 (11-25 23:28)阅读2回复0
lrj
lrj
  • 管理员
  • 注册排名2
  • 经验值131130
  • 级别管理员
  • 主题26226
  • 回复0
楼主

  本人比来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();

0
回帖

急求在vc++6.0中获取IPV6地址的办法,高手请进,谢谢!! 期待您的回复!

取消