我如何扫描所有IP地址范围/ s?

时间:2015-08-04 17:56:39

标签: java android android-studio

在MainActivity顶部,我添加了数组变量:

"2" + "3" == "23"

然后点击一下按钮,我使用function addFunction() { var x = parseInt(document.getElementById("firstInput").value); var y = parseInt(document.getElementById("secondInput").value); var sum=x+y; alert('Sum is:'+sum); } 迭代每个private final String[] ipaddresses = new String[]{ "http://10.0.0.1:8098/?cmd=nothing", "http://10.0.0.3:8098/?cmd=nothing", "http://10.0.0.2:8098/?cmd=nothing"}; 。 我检查了我的PC上的Web服务器,PC上有哪个IP。 我向服务器发送命令并返回结果。

问题是,如何构建一个包含所有逻辑可用IP地址的阵列? 由于我的PC有时可能在IP 10.0.0.2上,当我重新启动电脑或移动到另一台电脑时,IP可能是其他类似10.0.0.3

这是我也循环IP的按钮代码

for loop

新课程:

IP address

2 个答案:

答案 0 :(得分:2)

从10.0.0.0到10.0.0.255生成一组IP地址就像迭代最后一段的有效数字列表一样简单:

List<String> addresses = new ArrayList<>();
int min = 0;
int max = 255;
for(int i = min; i <= max; i++) {
    String address = "10.0.0." + i;
    addresses.add(address);
}

现在addresses列表中包含所有有效的IP地址。可以通过调整最小值和最大值来缩小生成的地址,以缩小可用的地址范围。

答案 1 :(得分:2)

这是我的建议:

public class IpRange {
    private static String ipMain = "10.0.0.";
    private static int minIpRange = 2;
    private static int maxIpRange = 255;
    private static String[] ipAddresses = new String[maxIpRange];

    public static void main(String[] args) {
        for (int i = minIpRange; i < maxIpRange; i++) {
            String ipRange = ipMain + i;
            //IP range in Array
            ipAddresses[i] = ipRange;

            //if you need full IP range in Url, then unmark the following 2 lines and mark above line
            //String urlRange = "http://" + ipRange + ":8098/?cmd=nothing";
            //ipAddresses[i] = urlRange;
        }

        //testing array results
        for (int i = minIpRange; i < maxIpRange; i++)
            System.out.println(ipAddresses[i]);
    }
}

IP地址范围在Array

10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
etc.

如果需要,输出将像完整的Url一样(只需修改代码):

http://10.0.0.2:8098/?cmd=nothing
http://10.0.0.3:8098/?cmd=nothing
http://10.0.0.4:8098/?cmd=nothing
http://10.0.0.5:8098/?cmd=nothing
http://10.0.0.6:8098/?cmd=nothing
etc.

注意:该类用于演示,仅作为如何实现此任务的示例,由个人修改并在相关代码中实现它,以满足最终要求

修改 我已经优化了代码,这里是更新版本:

public class IpRange {
    private String ipMain = "10.0.0.";
    private int startRange = 2; // 2 = 10.0.0.2
    private int endRange = 5;   // 5 = 10.0.0.5
    private int ipRangeLength = endRange - startRange;
    private String urlParam = ":8098/?cmd=nothing";

    public static void main(String[] args) {

        IpRange ipRange = new IpRange();

        // true -> ip range url with parameters / false -> only ip range
        String[] results = ipRange.ipRangeGenerator(true);

        for (String output : results)
            System.out.println(output);

    }

    public String[] ipRangeGenerator(boolean link) {
        String[] ipAddresses = new String[ipRangeLength];
        String urlRange;

        for (int i = 0; i < ipRangeLength; i++) {
            String ipRange = ipMain + (startRange + i);
            urlRange = ipRange;
            if (link)
                urlRange = "http://" + ipRange + urlParam;
            ipAddresses[i] = urlRange;
        }
        return ipAddresses;
    }

}