cocos2dx获取本机全部ip地址。。。

前端之家收集整理的这篇文章主要介绍了cocos2dx获取本机全部ip地址。。。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用的是raknet中获取网卡的方式,支持linux,ios,安卓系统。未支持win。

#if defined(ANDROID)

@H_502_5@void UdpSocket::getHostIpByString( @H_502_5@ char ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ],@H_502_5@unsigned @H_502_5@int binaryAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )

{

@H_502_5@struct ifreq ifreqs[MAXIMUM_NUMBER_OF_INTERNAL_IDS];

@H_502_5@struct ifconf ifconf;

@H_502_5@struct in_addr bin_addr;

memset(&ifconf,0,@H_502_5@sizeof(ifconf));

ifconf.ifc_buf = (@H_502_5@char*) (ifreqs);

ifconf.ifc_len = @H_502_5@sizeof(ifreqs);

{ // get list of interfaces

@H_502_5@int sock = socket(AF_INET,SOCK_STREAM,0);

@H_502_5@if (sock < 0)

{

perror("Error creating socket");

@H_502_5@return;

}

@H_502_5@if ((ioctl(sock,SIOCGIFCONF,(@H_502_5@char*) &ifconf )) < 0 )

{

perror("Error returned from ioctl(SIOGIFCONF)");

ifconf.ifc_len = 0;

}

close(sock);

}

@H_502_5@int idx = 0;

@H_502_5@int iface_count = ifconf.ifc_len/@H_502_5@sizeof(@H_502_5@struct ifreq);

printf("Interfaces (%d):\n",iface_count);

@H_502_5@for( ; idx < iface_count; idx++)

{

@H_502_5@char ip_addr[ 16/*INET_ADDRSTRLEN */];

@H_502_5@struct sockaddr_in *b = (@H_502_5@struct sockaddr_in *) &(ifreqs[idx].ifr_addr);

@H_502_5@const @H_502_5@char* host = inet_ntop(AF_INET,& b->sin_addr,ip_addr,@H_502_5@sizeof ip_addr);

strcpy( ipList[idx],host );

@H_502_5@if (inet_aton(host,&bin_addr) == 0)

{

perror("inet_aton error");

@H_502_5@continue;

}

@H_502_5@else

{

binaryAddresses[idx] = bin_addr.s_addr;

}

printf("\t%-10s\t%s (%08x)\n",ifreqs[idx].ifr_name,ipList[idx],binaryAddresses[idx]);

}

@H_502_5@for ( ; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx )

{

ipList[idx][0]=0;

}

}

#else

@H_502_5@void UdpSocket::getHostIpByString(@H_502_5@char ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ],@H_502_5@unsigned @H_502_5@int binaryAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS])

{

@H_502_5@struct ifaddrs *ifaddr,*ifa;

@H_502_5@int family,s;

@H_502_5@char host[NI_MAXHOST];

@H_502_5@struct in_addr linux_in_addr;

@H_502_5@if (getifaddrs(&ifaddr) == -1) {

printf( "Error getting interface list\n");

}

@H_502_5@int idx = 0;

@H_502_5@for (ifa = ifaddr; ifa != @H_502_5@ NULL; ifa = ifa->ifa_next) {

@H_502_5@if (!ifa->ifa_addr) @H_502_5@continue;

family = ifa->ifa_addr->sa_family;

@H_502_5@if (family == AF_INET) {

s = getnameinfo(ifa->ifa_addr,@H_502_5@sizeof(@H_502_5@struct sockaddr_in),host,NI_MAXHOST,@H_502_5@NULL, 0,NI_NUMERICHOST);

@H_502_5@if (s != 0) {

printf ("getnameinfo() Failed: %s\n",gai_strerror(s));

}

printf ("IP address: %s\n",host);

@H_502_5@if (strcmp(host,"127.0.0.1")!=0)

{

strcpy( ipList[ idx ],host );

@H_502_5@if (inet_aton(host,&linux_in_addr) == 0) {

perror("inet_aton");

}

@H_502_5@else {

binaryAddresses[idx]=linux_in_addr.s_addr;

}

idx++;

}

}

}

@H_502_5@for ( ; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx )

{

ipList[idx][0]=0;

}

freeifaddrs(ifaddr);

}

#endif

原文链接:https://www.f2er.com/cocos2dx/344408.html

猜你在找的Cocos2d-x相关文章