不要列出所有ip lan

时间:2017-07-27 15:28:02

标签: java ip ping lan

我的局域网是:

  • 192.168.1.100 - printer
  • 192.168.1.101 - pc-mac
  • 192.168.1.102 - pc-win
  • 192.168.1.254 - router

我会在jList中列出所有ip。

我的代码是:

public void listIP(){
        try
              {

            InetAddress address;  
            DefaultListModel iplan = new DefaultListModel();
            jList1.setModel(iplan);
            InetAddress localhost = InetAddress.getLocalHost();
            iplan.addElement(localhost.getHostAddress());
            String ip;
            ip = localhost.getHostAddress();
            String currIP;
            for (int i = 0; i < 256; i++)
            {
                try
                {

                currIP = ip.substring(0,ip.length()-3)+String.valueOf(i);
                address = InetAddress.getByName(currIP);
                if (address.isReachable(50))
                {
                    iplan.addElement(address.getHostAddress());
                } 
                }
                catch (IOException e) { 
                      ex.PrintStackTrace();
                }


            } 

        } 
        catch (UnknownHostException ex) {
            ex.PrintStackTrace();
        }

但输出仅为:

192.168.1.100
192.168.1.101

虽然我可以ping:

ping -t 3 192.168.1.102
PING 192.168.1.102 (192.168.1.102): 56 data bytes
64 bytes from 192.168.1.102: icmp_seq=0 ttl=128 time=2.936 ms
64 bytes from 192.168.1.102: icmp_seq=1 ttl=128 time=11.346 ms
64 bytes from 192.168.1.102: icmp_seq=2 ttl=128 time=2.913 ms

--- 192.168.1.102 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 2.913/5.732/11.346/3.970 ms

ping -t 3 192.168.1.254
PING 192.168.1.254 (192.168.1.254): 56 data bytes
64 bytes from 192.168.1.254: icmp_seq=0 ttl=254 time=2.094 ms
64 bytes from 192.168.1.254: icmp_seq=1 ttl=254 time=2.316 ms
64 bytes from 192.168.1.254: icmp_seq=2 ttl=254 time=2.228 ms

--- 192.168.1.254 ping statistics ---
4 packets transmitted, 3 packets received, 25.0% packet loss
round-trip min/avg/max/stddev = 2.094/2.213/2.316/0.091 ms

我可以ping所有ip,但它只显示两个。 为什么? 我该如何解决? 有什么想法吗?

0 个答案:

没有答案