Java 在指定范围内连续循环
我正在制作一个Java应用程序,它将循环通过我的DHCP表并尝试连接到多个设备。我正在一个IP范围内循环,但希望在应用程序关闭之前持续循环该范围。 连续循环的最佳实践是什么?将Java 在指定范围内连续循环,java,loops,while-loop,infinite-loop,inetaddress,Java,Loops,While Loop,Infinite Loop,Inetaddress,我正在制作一个Java应用程序,它将循环通过我的DHCP表并尝试连接到多个设备。我正在一个IP范围内循环,但希望在应用程序关闭之前持续循环该范围。 连续循环的最佳实践是什么?将startip的值设置两次,然后在达到最大范围后将startip设置回原始值?以下是我目前拥有的: public void loopFTP(String startIP, String endIP, int timeout) throws SocketException, IOException { InetAdd
startip
的值设置两次,然后在达到最大范围后将startip
设置回原始值?以下是我目前拥有的:
public void loopFTP(String startIP, String endIP, int timeout) throws SocketException, IOException {
InetAddress startAsIP = InetAddresses.forString(startIP);
InetAddress endAsIP = InetAddresses.forString(endIP);
while(InetAddresses.coerceToInteger(startAsIP) <= InetAddresses.coerceToInteger(endAsIP)){
System.out.println(startAsIP);
attemptConnection(startAsIP, timeout);
startAsIP = InetAddresses.increment(startAsIP);
}
}
public void loopFTP(String startIP、String endIP、int timeout)抛出SocketException、IOException{
InetAddress startAsIP=InetAddresses.forString(startIP);
InetAddress endAsIP=InetAddresses.forString(endIP);
while(InetAddresses.impresseToInteger(startAsIP)如果循环应该是无限的,那么可以使用for(;;)
或while(true)
循环
当到达范围结束时,只需根据startIP
值重置startAsIP
:
public void loopFTP(String startIP, String endIP, int timeout) throws SocketException, IOException {
InetAddress startAsIP = InetAddresses.forString(startIP);
InetAddress endAsIP = InetAddresses.forString(endIP);
while(true){
System.out.println(startAsIP);
attemptConnection(startAsIP, timeout);
if(InetAddresses.coerceToInteger(startAsIP) <= InetAddresses.coerceToInteger(endAsIP))
startAsIP = InetAddresses.increment(startAsIP);
else
startAsIP = InetAddresses.forString(startIP);
}
}
public void loopFTP(String startIP、String endIP、int timeout)抛出SocketException、IOException{
InetAddress startAsIP=InetAddresses.forString(startIP);
InetAddress endAsIP=InetAddresses.forString(endIP);
while(true){
系统输出打印LN(startAsIP);
尝试连接(startAsIP,超时);
if(InetAddresses.ImpresseToInteger(startAsIP)如果循环应该是无限的,则可以使用for(;;)
或while(true)
循环
当到达范围结束时,只需根据startIP
值重置startAsIP
:
public void loopFTP(String startIP, String endIP, int timeout) throws SocketException, IOException {
InetAddress startAsIP = InetAddresses.forString(startIP);
InetAddress endAsIP = InetAddresses.forString(endIP);
while(true){
System.out.println(startAsIP);
attemptConnection(startAsIP, timeout);
if(InetAddresses.coerceToInteger(startAsIP) <= InetAddresses.coerceToInteger(endAsIP))
startAsIP = InetAddresses.increment(startAsIP);
else
startAsIP = InetAddresses.forString(startIP);
}
}
public void loopFTP(String startIP、String endIP、int timeout)抛出SocketException、IOException{
InetAddress startAsIP=InetAddresses.forString(startIP);
InetAddress endAsIP=InetAddresses.forString(endIP);
while(true){
系统输出打印LN(startAsIP);
尝试连接(startAsIP,超时);
if(InetAddresses.ImpresseToInteger(startAsIP)while(true)
不够吗?不确定这里是否有“最佳实践”。任何有效的都应该足够好。if(InetAddresses.ImpresseToInteger)while(true)
不够吗?不确定这里是否有“最佳实践”。任何有效的都应该足够好。这很完美。谢谢。这很完美。谢谢