我如何扫描所有的IP-地址范围/ s的? [英] How I scan all the IP-Addresses range/s?

查看:262
本文介绍了我如何扫描所有的IP-地址范围/ s的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MainActivity顶我添加数组变量:

 私人最终的String [] = ipaddresses新的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};

然后用一个按钮,单击我使用循环来遍历每个 IP地址
我检查我的电脑上的IP的PC是坐在上的Web服务器。
我将命令发送到服务器,并得到一个结果。

现在的问题是,我怎么能建立与所有的逻辑可用的IP地址的数组?
因为我的电脑可有时IP 10.0.0.2,当我重新启动电脑或移动到另一台电脑的IP地址可以是别的东西像10.0.0.3

这是按钮code,其中我也遍历所有的IP地址

 公共无效addListenerOnButton()
    {        btnClick =(按钮)findViewById(R.id.checkipbutton);        btnClick.setOnClickListener(新OnClickListener()
        {
            字节[]响应= NULL;
            @覆盖
            公共无效的onClick(查看arg0中)
            {                文字=(TextView的)findViewById(R.id.textView2);
                线程t =新主题(新的Runnable()
                {
                    @覆盖
                    公共无效的run()
                    {
                        的for(int i = 0; I< ipaddresses.length;我++)                        {                                尝试
                                {
                                    响应=获取(ipaddresses [I]);
                                }
                                赶上(例外五)
                                {
                                    串ERR = e.toString();
                                }                                如果(响应!= NULL)
                                {                                    尝试
                                    {
                                        最后一个字符串=新的String(响应,UTF-8);
                                        text.post(新的Runnable()
                                        {
                                            @覆盖
                                            公共无效的run()
                                            {
                                                text.setText(一);
                                            }
                                        });                                        Logger.getLogger(MainActivity(内螺纹))的信息(一)。
                                    }赶上(UnsupportedEncodingException五)
                                    {
                                        e.printStackTrace();
                                        Logger.getLogger(MainActivity(内螺纹))的信息(编码异常)。
                                    }                                    Logger.getLogger(MainActivity(内螺纹))的信息(测试1)。
                                    打破;                                }                                其他
                                {                                }                            text.post(新的Runnable()
                            {
                                @覆盖
                                公共无效的run()
                                {
                                    text.setText(连接失败);
                                }
                            });                        }                    }
                });
                t.start();
            }
        });    }

新类:

 包com.test.webservertest;/ **
 *创建者bout0于2015年8月4日。
 * /
公共类IpAddresses
{
        私人静态字符串ipMain =10.0.0。
        私有静态诠释minIpRange = 0;
        私有静态诠释maxIpRange = 255;
        私有静态的String [] = ipAddresses新的String [maxIpRange]        公共静态无效的主要(字串[] args){
            的for(int i = minIpRange; I< maxIpRange;我++){
                字符串ipRange = ipMain + I;
                // IP范围阵列
                // ipAddresses [I] = ipRange;                //如果你需要在网址全IP范围,则取消标记以下2线和关口上方线
                字符串UrlRange =HTTP://+ ipRange +:8098 / CMD =无中生有;
                ipAddresses [I] = UrlRange;
            }            //测试结果数组
            的for(int i = minIpRange; I< maxIpRange;我++)
                的System.out.println(ipAddresses [I]);
        }}


解决方案

下面是我的建议:

 公共类IpRange {
    私人静态字符串ipMain =10.0.0。
    私有静态诠释minIpRange = 2;
    私有静态诠释maxIpRange = 255;
    私有静态的String [] = ipAddresses新的String [maxIpRange]    公共静态无效的主要(字串[] args){
        的for(int i = minIpRange; I< maxIpRange;我++){
            字符串ipRange = ipMain + I;
            // IP范围阵列
            ipAddresses [I] = ipRange;            //如果你需要在网址全IP范围,则取消标记以下2线和关口上方线
            //字符串urlRange =HTTP://+ ipRange +:8098 / CMD =什么?
            // ipAddresses [I] = urlRange;
        }        //测试结果数组
        的for(int i = minIpRange; I< maxIpRange;我++)
            的System.out.println(ipAddresses [I]);
    }
}

IP地址范围阵列

  10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
等等

或者输出会是这样的完整的URL(如有需要只需修改code如前所述):

  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
等等

注意:的类示范制成,仅仅是例子这个任务如何可以实现的,它是由个人来修改它,实现它的相对code,履行最后的要求。


修改
因为我已经在这里进行了优化的code为更新版本:

 公共类IpRange {
    私人字符串ipMain =10.0.0。
    私人INT startRange = 2; // 2 = 10.0.0.2
    私人INT endRange = 5; // 5 = 10.0.0.5
    私人INT ipRangeLength = endRange - startRange;
    私人字符串urlParam =:8098 / CMD =无中生有;    公共静态无效的主要(字串[] args){        IpRange ipRange =新IpRange();        //真 - >带参数/假的IP地址范围 - >只有IP范围
        的String [] =结果ipRange.ipRangeGenerator(真);        对于(字符串输出:结果)
            的System.out.println(输出);    }    公众的String [] ipRangeGenerator(布尔链接){
        的String [] = ipAddresses新的String [ipRangeLength]
        串urlRange;        的for(int i = 0; I< ipRangeLength;我++){
            字符串ipRange = ipMain +(startRange + I);
            urlRange = ipRange;
            如果(链接)
                urlRange =HTTP://+ ipRange + urlParam;
            ipAddresses [I] = urlRange;
        }
        返回ipAddresses;
    }}

In MainActivity top I added array variable:

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

Then with a button click I use a for loop to iterate over each IP address. I check with a web server on my PC on which IP the PC is sitting. I send a command to the server and get back a result.

The question is, how I can build an array with all the logic available IP addresses? Since my PC can be sometimes on IP 10.0.0.2 and when I restart the pc or move to another PC the IP can be something else like 10.0.0.3

This is the button code where I also loop over the IPs

public void addListenerOnButton()
    {

        btnClick = (Button) findViewById(R.id.checkipbutton);

        btnClick.setOnClickListener(new OnClickListener()
        {
            byte[] response = null;
            @Override
            public void onClick(View arg0)
            {

                text = (TextView) findViewById(R.id.textView2);


                Thread t = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        for (int i = 0; i < ipaddresses.length; i++)

                        {

                                try
                                {
                                    response = Get(ipaddresses[i]);
                                }
                                catch (Exception e)
                                {
                                    String err = e.toString();
                                }

                                if (response!=null)
                                {



                                    try
                                    {
                                        final String a = new String(response,"UTF-8");




                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                text.setText(a);
                                            }
                                        });

                                        Logger.getLogger("MainActivity(inside thread)").info(a);
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                        Logger.getLogger("MainActivity(inside thread)").info("encoding exception");
                                    }

                                    Logger.getLogger("MainActivity(inside thread)").info("test1");
                                    break;

                                }

                                else
                                {

                                }

                            text.post(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    text.setText("Connection Failed");
                                }
                            });



                        }

                    }
                });
                t.start();
            }
        });

    }

New class:

package com.test.webservertest;

/**
 * Created by bout0 on 8/4/2015.
 */
public class IpAddresses
{
        private static String ipMain = "10.0.0.";
        private static int minIpRange = 0;
        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]);
        }

}

解决方案

Here is my suggestion:

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 addresses range in Array

10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
etc.

Or the output will be like this as complete Url if required (just modify the code as noted):

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.

Note: the class is made for demonstration and is just example how this task can be achieved, it is up the individual to modified it and implement it in the relative code, to fulfill the final requirement.


EDIT I have since optimized the code here is updated version:

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

}

这篇关于我如何扫描所有的IP-地址范围/ s的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆