获得手机的ip
本文轉載至 http://blog.csdn.net/showhilllee/article/details/8746114
iosip手機
貌似ASI里獲取ip地址的鏈接不可以了。也曾試過whatismyip,在其網站上的截圖獲取的ip是正確的,單不知道為什么在我這里卻是錯誤的。所以,在這里分享一下獲得手機ip的方法。代碼最初源自老外,通過系統提供的api獲取的,這里我就直接貼代碼了:
在創建的獲得ip類的頭文件里:
[java]view plaincopyprint?
#defineMAXADDRS32
externchar*if_names[MAXADDRS];
externchar*ip_names[MAXADDRS];
externchar*hw_addrs[MAXADDRS];
externunsignedlongip_addrs[MAXADDRS];
//Functionprototypes
voidInitAddresses();
voidFreeAddresses();
voidGetIPAddresses();
voidGetHWAddresses();
實現文件里:
先導入文件
[java]view plaincopyprint?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/sockio.h>
#include<net/if.h>
#include<errno.h>
#include<net/if_dl.h>
具體實現的代碼
[java]view plaincopyprint?
#definemin(a,b)((a)<(b)?(a):(b))
#definemax(a,b)((a)>(b)?(a):(b))
#defineBUFFERSIZE4000
char*if_names[MAXADDRS];
char*ip_names[MAXADDRS];
char*hw_addrs[MAXADDRS];
unsignedlongip_addrs[MAXADDRS];
staticintnextAddr=0;
voidInitAddresses()
{
inti;
for(i=0;i<MAXADDRS;++i)
{
if_names[i]=ip_names[i]=hw_addrs[i]=NULL;
ip_addrs[i]=0;
}
}
voidFreeAddresses()
{
inti;
for(i=0;i<MAXADDRS;++i)
{
if(if_names[i]!=0)free(if_names[i]);
if(ip_names[i]!=0)free(ip_names[i]);
if(hw_addrs[i]!=0)free(hw_addrs[i]);
ip_addrs[i]=0;
}
InitAddresses();
}
voidGetIPAddresses()
{
inti,len,flags;
charbuffer[BUFFERSIZE],*ptr,lastname[IFNAMSIZ],*cptr;
structifconfifc;
structifreq*ifr,ifrcopy;
structsockaddr_in*sin;
chartemp[80];
intsockfd;
for(i=0;i<MAXADDRS;++i)
{
if_names[i]=ip_names[i]=NULL;
ip_addrs[i]=0;
}
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
perror("socketfailed");
return;
}
ifc.ifc_len=BUFFERSIZE;
ifc.ifc_buf=buffer;
if(ioctl(sockfd,SIOCGIFCONF,&ifc)<0)
{
perror("ioctlerror");
return;
}
lastname[0]=0;
for(ptr=buffer;ptr<buffer+ifc.ifc_len;)
{
ifr=(structifreq*)ptr;
len=max(sizeof(structsockaddr),ifr->ifr_addr.sa_len);
ptr+=sizeof(ifr->ifr_name)+len;//fornextoneinbuffer
if(ifr->ifr_addr.sa_family!=AF_INET)
{
continue;//ignoreifnotdesiredaddressfamily
}
if((cptr=(char*)strchr(ifr->ifr_name,':'))!=NULL)
{
*cptr=0;//replacecolonwillnull
}
if(strncmp(lastname,ifr->ifr_name,IFNAMSIZ)==0)
{
continue;/*alreadyprocessedthisinterface*/
}
memcpy(lastname,ifr->ifr_name,IFNAMSIZ);
ifrcopy=*ifr;
ioctl(sockfd,SIOCGIFFLAGS,&ifrcopy);
flags=ifrcopy.ifr_flags;
if((flags&IFF_UP)==0)
{
continue;//ignoreifinterfacenotup
}
if_names[nextAddr]=(char*)malloc(strlen(ifr->ifr_name)+1);
if(if_names[nextAddr]==NULL)
{
return;
}
strcpy(if_names[nextAddr],ifr->ifr_name);
sin=(structsockaddr_in*)&ifr->ifr_addr;
strcpy(temp,inet_ntoa(sin->sin_addr));
ip_names[nextAddr]=(char*)malloc(strlen(temp)+1);
if(ip_names[nextAddr]==NULL)
{
return;
}
strcpy(ip_names[nextAddr],temp);
ip_addrs[nextAddr]=sin->sin_addr.s_addr;
++nextAddr;
}
close(sockfd);
}
voidGetHWAddresses()
{
structifconfifc;
structifreq*ifr;
inti,sockfd;
charbuffer[BUFFERSIZE],*cp,*cplim;
chartemp[80];
for(i=0;i<MAXADDRS;++i)
{
hw_addrs[i]=NULL;
}
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
perror("socketfailed");
return;
}
ifc.ifc_len=BUFFERSIZE;
ifc.ifc_buf=buffer;
if(ioctl(sockfd,SIOCGIFCONF,(char*)&ifc)<0)
{
perror("ioctlerror");
close(sockfd);
return;
}
ifr=ifc.ifc_req;
cplim=buffer+ifc.ifc_len;
for(cp=buffer;cp<cplim;)
{
ifr=(structifreq*)cp;
if(ifr->ifr_addr.sa_family==AF_LINK)
{
structsockaddr_dl*sdl=(structsockaddr_dl*)&ifr->ifr_addr;
inta,b,c,d,e,f;
inti;
strcpy(temp,(char*)ether_ntoa(LLADDR(sdl)));
sscanf(temp,"%x:%x:%x:%x:%x:%x",&a,&b,&c,&d,&e,&f);
sprintf(temp,"%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);
for(i=0;i<MAXADDRS;++i)
{
if((if_names[i]!=NULL)&&(strcmp(ifr->ifr_name,if_names[i])==0))
{
if(hw_addrs[i]==NULL)
{
hw_addrs[i]=(char*)malloc(strlen(temp)+1);
strcpy(hw_addrs[i],temp);
break;
}
}
}
}
cp+=sizeof(ifr->ifr_name)+max(sizeof(ifr->ifr_addr),ifr->ifr_addr.sa_len);
}
close(sockfd);
}
然后就是調用了,首先在我們的類里面導入上面那個類,之后在實現文件里寫函數
[java]view plaincopyprint?
-(NSString*)deviceIPAdress{
InitAddresses();
GetIPAddresses();
GetHWAddresses();
return[NSStringstringWithFormat:@"%s",ip_names[1]];
}
然后在我們需要獲得ip的地方寫上
[java]view plaincopyprint?
NSString*myip=[selfdeviceIPAdress];
這里的myip就是我們需要的ip地址了
手機截圖
程序打印截圖
總結
- 上一篇: 什么是JavaEE,什么是Spring
- 下一篇: 常用工具总结