苹果于6.1要求上架appStore的应用全都需要支持ipv6 only。
而本人的应用主要是基于UDP和TCP通信的。所以大量使用了底层。
现在按照苹果设置ipv6环境的步骤,使本人的手机设置成了ipv6环境,其它网络没变。
假如什么都不改,肯定通信失败。
然后本人就初始化了一个ipv6的socket
m_udpSocket = socket(AF_INET6, SOCK_DGRAM,IPPROTO_UDP);
然后调用以下发送代码:
struct addrinfo address, *res;
const char* ipAddress = pkt->destIp;
if(NULL == ipAddress){
LOGE(TAG,” dest ip is NULL..”);
return;
}
LOGI(TAG, “target ip:%s,port:%d”,ipAddress,pkt->destPort);
memset(&address, 0, sizeof(address));
address.ai_family = AF_UNSPEC;
address.ai_socktype = SOCK_DGRAM;
address.ai_flags = AI_DEFAULT;
address.ai_protocol = IPPROTO_UDP;
char ip[20];
if (strcmp(ipAddress, “255.255.255.255”) == 0) {
ipAddress = “FF02::1”; //组播地址
}
char portStr[5];
sprintf(portStr, “%d”,pkt->destPort);
int result = getaddrinfo(ipAddress, portStr, &address, &res);
//假如转换失败
if( 0 != result){
LOGE(TAG,”convert ip address failed..”);
return;
}
int headLen = m_headLen;
if(pkt->isRemote)
headLen = m_remoteLen;
int len = headLen + pkt->dataLen;
uint8 buff[len+1];
buildFrame(pkt, buff);
printHexString(buff, len);
ssize_t sendSize = sendto(m_udpSocket, buff, (size_t)len, 0,res->ai_addr, res->ai_addrlen);
if( -1 == sendSize){
LOGE(TAG,”send data failed!!!..”);
}
但假如ipAddress为ipv4的ip地址时,则sendto将会发送失败。假如是ipv6的地址的话,则能够发送成功。(暂不讨论它发到了哪里,有没有响应)。
但是本人这只能得到一个ipv4的地址。这种情况下,本人能够正常通信么?socket要怎么改呢?
而本人的应用主要是基于UDP和TCP通信的。所以大量使用了底层。
现在按照苹果设置ipv6环境的步骤,使本人的手机设置成了ipv6环境,其它网络没变。
假如什么都不改,肯定通信失败。
然后本人就初始化了一个ipv6的socket
m_udpSocket = socket(AF_INET6, SOCK_DGRAM,IPPROTO_UDP);
然后调用以下发送代码:
struct addrinfo address, *res;
const char* ipAddress = pkt->destIp;
if(NULL == ipAddress){
LOGE(TAG,” dest ip is NULL..”);
return;
}
LOGI(TAG, “target ip:%s,port:%d”,ipAddress,pkt->destPort);
memset(&address, 0, sizeof(address));
address.ai_family = AF_UNSPEC;
address.ai_socktype = SOCK_DGRAM;
address.ai_flags = AI_DEFAULT;
address.ai_protocol = IPPROTO_UDP;
char ip[20];
if (strcmp(ipAddress, “255.255.255.255”) == 0) {
ipAddress = “FF02::1”; //组播地址
}
char portStr[5];
sprintf(portStr, “%d”,pkt->destPort);
int result = getaddrinfo(ipAddress, portStr, &address, &res);
//假如转换失败
if( 0 != result){
LOGE(TAG,”convert ip address failed..”);
return;
}
int headLen = m_headLen;
if(pkt->isRemote)
headLen = m_remoteLen;
int len = headLen + pkt->dataLen;
uint8 buff[len+1];
buildFrame(pkt, buff);
printHexString(buff, len);
ssize_t sendSize = sendto(m_udpSocket, buff, (size_t)len, 0,res->ai_addr, res->ai_addrlen);
if( -1 == sendSize){
LOGE(TAG,”send data failed!!!..”);
}
但假如ipAddress为ipv4的ip地址时,则sendto将会发送失败。假如是ipv6的地址的话,则能够发送成功。(暂不讨论它发到了哪里,有没有响应)。
但是本人这只能得到一个ipv4的地址。这种情况下,本人能够正常通信么?socket要怎么改呢?
解决方案
10
看下google的这个库吧:CocoaAsyncSocket:https://github.com/robbiehanson/CocoaAsyncSocket 里面有代码展示:Socket怎么样同时支持IPV4和IPV6。例如:
…
if(address4) *address4 = [NSData dataWithBytes:&nativeAddr length:sizeof(nativeAddr)];
if(address6) *address6 = [NSData dataWithBytes:&nativeAddr6 length:sizeof(nativeAddr6)];
…
…
if(address4) *address4 = [NSData dataWithBytes:&nativeAddr length:sizeof(nativeAddr)];
if(address6) *address6 = [NSData dataWithBytes:&nativeAddr6 length:sizeof(nativeAddr6)];
…
60
http://blog.csdn.net/diyal/article/details/51645207