获取路由器的ip

来源:互联网 发布:华视微讯网络摄像机 编辑:程序博客网 时间:2024/04/20 00:31

最近做的项目需要获取路由器地址,查了很多资料以下方法终于成功了。
需要引用的头文件  

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1.    
  2. #include <sys/socket.h>  
  3. #import <netinet/in.h>  
  4. #import <netinet6/in6.h>  
  5. #import <arpa/inet.h>  
  6. #import <ifaddrs.h>  
  7. #include <netdb.h>  
  8. #import <SystemConfiguration/SCNetworkReachability.h>   
  9. #import "getgateway.h"  
  10. #import <arpa/inet.h>  


具体实现

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (NSString *) routerIp {  
  2.       
  3.     NSString *address = @"error";  
  4.     struct ifaddrs *interfaces = NULL;  
  5.     struct ifaddrs *temp_addr = NULL;  
  6.     int success = 0;  
  7.       
  8.       
  9.       
  10.       
  11.     // retrieve the current interfaces - returns 0 on success  
  12.     success = getifaddrs(&interfaces);  
  13.     if (success == 0)  
  14.     {  
  15.         // Loop through linked list of interfaces  
  16.         temp_addr = interfaces;  
  17.         //*/  
  18.         while(temp_addr != NULL)  
  19.         /*/  
  20.          int i=255;  
  21.          while((i--)>0)  
  22.          //*/  
  23.         {  
  24.             if(temp_addr->ifa_addr->sa_family == AF_INET)  
  25.             {  
  26.                 // Check if interface is en0 which is the wifi connection on the iPhone  
  27.                 if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])  
  28.                 {  
  29.                     // Get NSString from C String //ifa_addr  
  30.                     //ifa->ifa_dstaddr is the broadcast address, which explains the "255's"  
  31.                     //                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)];  
  32.                       
  33.                     address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];  
  34.                       
  35.                     //routerIP----192.168.1.255 广播地址  
  36.                     NSLog(@"broadcast address--%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)]);  
  37.                     //--192.168.1.106 本机地址  
  38.                     NSLog(@"local device ip--%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]);  
  39.                     //--255.255.255.0 子网掩码地址  
  40.                     NSLog(@"netmask--%@",[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)]);  
  41.                     //--en0 端口地址  
  42.                     NSLog(@"interface--%@",[NSString stringWithUTF8String:temp_addr->ifa_name]);  
  43.                       
  44.                 }  
  45.                   
  46.             }  
  47.               
  48.             temp_addr = temp_addr->ifa_next;  
  49.         }  
  50.     }  
  51.       
  52.     // Free memory  
  53.     freeifaddrs(interfaces);  
  54.       
  55.       
  56.       
  57.     in_addr_t i =inet_addr([address cStringUsingEncoding:NSUTF8StringEncoding]);  
  58.     in_addr_t* x =&i;  
  59.       
  60.       
  61.     unsigned charchar *s=getdefaultgateway(x);  
  62.     NSString *ip=[NSString stringWithFormat:@"%d.%d.%d.%d",s[0],s[1],s[2],s[3]];  
  63.       
  64.     NSLog(@"路由器地址-----%@",ip);  
  65.     return ip;  
  66. }  


0 0