gpt4 book ai didi

java - 如何计算下一个具有 ip 位表示形式的 IP 地址?

转载 作者:行者123 更新时间:2023-12-01 15:11:32 25 4
gpt4 key购买 nike

我从 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
}

最佳答案

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++;
}

关于java - 如何计算下一个具有 ip 位表示形式的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12282986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com