使用ipv6地址绑定()api失败

时间:2012-01-16 04:55:10

标签: ipv6

当我尝试绑定到我的自动配置链接本地地址时,它可以正常工作。但是当我尝试绑定到一些手动分配的IPv6地址时,bind()失败并出现10049错误。这是代码

bool StartServer()
{
  const char *Ip = " fe80::216:76ff:fe12:2661%4";
  SHORT nIPFamily = -1;
  int dSock;
  union 
 {
   struct sockaddr_in  saddr4;
   struct sockaddr_in6  saddr6;
 }ServerIP;

memset的(安培; SERVERIP,0,的sizeof(SERVERIP));

 if(strstr(Ip,":"))
 {
   UCHAR MyIp[16];
   USHORT ScopeId = 0;
   ServerIP.saddr6.sin6_family = nIPFamily = AF_INET6;
   ServerIP.saddr6.sin6_port = htons( nPort );
   StringToAddress( Ip, MyIp ); //common function to convert string to address (16byte)
   memcpy( ServerIP.saddr6.sin6_addr.u.Byte, MyIp,16 );
   sscanf(strstr(Ip,"%")+1,"%d",&ScopeId);
   ServerIP.saddr6.sin6_scope_id = ScopeId;
   nAddLen = sizeof(sockaddr_in6);
   psockaddr = &ServerIP.saddr6;
 }

 if ((dSock = socket(nIPFamily, SOCK_STREAM, 0)) < 0)
 {
   AfxMessageBox("Failed to create server socket");
   return -1;
 }
 if (bind (dSock, (sockaddr*)psockaddr, nAddLen) < 0)
 {
   char errmsg[128];
   sprintf(errmsg,"Bind Failed with error %d",WSAGetlastError());
   AfxMessageBox(errmsg);
   return -1;
 }

请建议。我现在坚持了2天以上!!!

2 个答案:

答案 0 :(得分:0)

建议:

问:in6addr_any有效吗?

问:你碰巧在支持ipv6的PC上运行(例如运行Vista或Win7)吗?

答案 1 :(得分:0)

只有链接本地地址使用范围ID,在使用全局单播时不添加它