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

根据 Robert 的建议,一个简单的解决方案是测试你的 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 回答