这个问题已经纠缠好些天了,郁闷啊..
感觉winsock中对ipv6的地址支持的并不好,虽然官方的解释是getaddrinfo和getnameinfo已经可以完成所有的地址解析工作,但是个人觉得用起来很不方便,如inet_ntoa之类的简单转换都很难实现,哪位高手讲一讲吧,下面是代码:
         char szHostName[256];
gethostname(szHostName,256);
struct addrinfo hints2, *res2=NULL;
memset(&hints2,0,sizeof(hints2));
hints2.ai_flags=AI_CANONNAME;
hints2.ai_family=AF_INET6;
rc=getaddrinfo(szHostName,"10000",&hints2,&res2);
char *v6IP=res2->ai_addr->sa_data;
         char hostName[256];
char servInfo[256];
if ((getnameinfo(res2->ai_addr, 
             res2->ai_addrlen, 
 hostName, 
 256, 
 servInfo, 
 256, 
 NI_NUMERICHOST | NI_NUMERICSERV)) != 0) 
{
printf("getnameinfo() failed.\n");
printf("Error #: %ld\n", WSAGetLastError());
return 0;
}
这样只能获得本机的回路地址,即::1
怎样获得本机的链路本地和全球单播地址呢,希望是和网卡无关的,即不需要选择网卡index,十分感谢!

解决方案 »

  1.   

    再说明一下,我的系统是XP,记得在windows 2003下使用上述方法可以获得所有的ipv6地址列表.
      

  2.   

    链路地址是MAC地址吧,单播是IP吧
      

  3.   

    以上得到的也只是ipv4地址。目前还没有任何一个windows版本提供以ipv6的支持。
      

  4.   

    提供以ipv6的支持。
    ->
    提供对ipv6的支持。
      

  5.   

    师兄告诉了一个获得链路本地的方法:通过获得Mac地址按照地址转换方法可以得到链路本地地址.终于有点突破了:)
      

  6.   

    下载PLATFORM SDK#include <IPExport.h>
    void l2ip(const struct in6_addr& ip, std::string& str)
    {
    char slask[100]; // l2ip temporary
    *slask = 0;
    unsigned int prev = 0;
    bool skipped = false;
    bool ok_to_skip = true; {
    for (size_t i = 0; i < 16; i++)
    {
    unsigned int x = ntohs(ip.u.Byte[i]);
    if (*slask && (x || !ok_to_skip || prev))
    strcat(slask,":");
    if (x || !ok_to_skip)
    {
    sprintf(slask + strlen(slask),"%X", x);
    if (x && skipped)
    ok_to_skip = false;
    }
    else
    {
    skipped = true;
    }
    prev = x;
    }
    }
    str = slask;
    }void GetLocalIpV6( char * szIPV6Buffer )
    {
    char name[256];

    *name = 0;
    PHOSTENT hostinfo;
    if( gethostname ( name, sizeof(name)) == 0)
    {
    if((hostinfo = gethostbyname(name)) == NULL)
    {
    //error;....
    }
    }

    struct in6_addr local_ip6;
    memset(&local_ip6, 0, sizeof(local_ip6)); memcpy(&local_ip6,hostinfo -> h_addr_list[0],hostinfo -> h_length);

    string s;
    l2ip( local_ip6, s ); strcpy( szIPV6Buffer, s.c_str() );
    }
      

  7.   

    striking你好!
    我用的是VS2005的SDK,但是编译的时候还是有问题,不知道应该用那个版本的?
    还有就是想请教你PLATFORM SDK应该在哪下载?好像microsoft的网站上太慢,不可能下下来吧
    麻烦你回答菜鸟问题:)
      

  8.   

    e:\software\platformsdk\include\ipexport.h(23) : error C2146: syntax error : missing ';' before identifier 'IPAddr'
    e:\software\platformsdk\include\ipexport.h(23) : fatal error C1004: unexpected end of file found
    这个文件夹中的platformsdk是从VS2005中拷的,并且已经在directories中设置为第一个.
    还想请教你一个问题,我得到局域网中的其他主机的链路本地地址以后,再用connect,为什么除了本机以外其他的地址都不可达,不知道你有这方面的经验没有,谢谢啦!!:)
      

  9.   

    #ifndef  aaaaaaaaa
    #define  aaaaaaaaavoid l2ip(const struct in6_addr& ip, std::string& str)
    ....
    void GetLocalIpV6( char * szIPV6Buffer );
    ...#endif
      

  10.   

    你的cpp 有没有 include  stdafx.h