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

cocos2dx获取本机全部ip地址。。。,第1张

概述使用的是raknet中获取网卡的方式,支持linux,ios,安卓系统。未支持win。 #if defined(ANDROID) void UdpSocket::getHostIpByString( char ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ], unsigned int binaryAddresses[MAXIMUM_NUMBER_OF_

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

#if defined(ANDROID)

voID UdpSocket::getHostIpByString( char ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ],unsigned int binaryAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )

{

struct ifreq ifreqs[MAXIMUM_NUMBER_OF_INTERNAL_IDS];

struct ifconf ifconf;

struct in_addr bin_addr;

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

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

ifconf.ifc_len = sizeof(ifreqs);

{ // get List of interfaces

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

if (sock < 0)

{

perror("Error creating socket");

return;

}

if ((ioctl(sock,SIocgIFCONF,(char*) &ifconf )) < 0 )

{

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

ifconf.ifc_len = 0;

}

close(sock);

}

int IDx = 0;

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

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

for( ; IDx < iface_count; IDx++)

{

char ip_addr[ 16/*INET_ADDRSTRLEN */];

struct sockaddr_in *b = (struct sockaddr_in *) &(ifreqs[IDx].ifr_addr);

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

strcpy( ipList[IDx],host );

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

{

perror("inet_aton error");

continue;

}

else

{

binaryAddresses[IDx] = bin_addr.s_addr;

}

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

}

for ( ; IDx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++IDx )

{

ipList[IDx][0]=0;

}

}

#else

voID UdpSocket::getHostIpByString(char ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ],unsigned int binaryAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS])

{

struct ifaddrs *ifaddr,*ifa;

int family,s;

char host[NI_MAXHOST];

struct in_addr linux_in_addr;

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

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

}

int IDx = 0;

for (ifa = ifaddr; ifa != NulL; ifa = ifa->ifa_next) {

if (!ifa->ifa_addr) continue;

family = ifa->ifa_addr->sa_family;

if (family == AF_INET) {

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

if (s != 0) {

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

}

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

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

{

strcpy( ipList[ IDx ],host );

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

perror("inet_aton");

}

else {

binaryAddresses[IDx]=linux_in_addr.s_addr;

}

IDx++;

}

}

}

for ( ; IDx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++IDx )

{

ipList[IDx][0]=0;

}

freeifaddrs(ifaddr);

}

#endif

总结

以上是内存溢出为你收集整理的cocos2dx获取本机全部ip地址。。。全部内容,希望文章能够帮你解决cocos2dx获取本机全部ip地址。。。所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1040247.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存