2012-09-05 23 views
0

我從DHCP信息獲得IP地址。當我以比特表示IP時,如何計算下一個IP地址。如何計算下一個IP地址具有位表示的IP?

WifiManager wifii = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
DhcpInfo d = wifii.getDhcpInfo(); 
int mask = d.netmask; 
int ip0 = d.ipAddress & d.netmask; 
int num = ~d.netmask; //it should be correct but don't work. why? 

//this don't work. How make it correct? 
for(int ip = ip0; ip < ip + num; ip++){ 
    //here ip next ip 
} 
+0

字節順序未正確記錄。也許這不是你所期望的(litte endian vs big endian)。你看過你實際得到的價值嗎?他們可能會給出一個線索...... – user1252434

+0

我知道掩碼00000000.11111111.11111111.11111111是255.255.255.0 – LunaVulpo

+0

你必須將其逆轉,然後 – njzk2

回答

0

一個簡單的解決方案,立足羅伯特的建議,將是從你的向上和向下測試IPS並進行測試:

int ip = d.ipAddress; 
while (ip & d.netmask) { 
    // Valid ip 
    ip++ 
} 
ip = d.ipAddress - 1; 
while (ip & d.netmask) { 
    // Valid ip 
    ip-- 
} 
1

舉例IP = 192.168.1.16和網絡掩碼255.255.255.0:

int ipAddress = 0xC0A80110; 
int mask = 0xFFFFFF00; 
int maskedIp = ipAddress & mask; 
int ip = ipAddress; 
// Loop until we have left the unmasked region 
while ((mask & ip) == maskedIp) { 
    printIP(ip); 
    ip++; 
} 
相關問題