0

DHCP情報から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
}
4

2 に答える 2

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++;
}
于 2012-09-05T14:12:13.103 に答える
0

ロバートの提案に基づく簡単な解決策は、あなたのものからipsを上下にテストし、それらをテストすることです:

int ip = d.ipAddress;
while (ip & d.netmask) {
    // Valid ip
    ip++
}
ip = d.ipAddress - 1;
while (ip & d.netmask) {
    // Valid ip
    ip--
}
于 2012-09-05T14:22:00.083 に答える