0

My LAN is:

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

I would list all ip in a jList.

My code is:

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

but the output is only:

192.168.1.100
192.168.1.101

while i can 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

and

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

I can ping all ip but it show only two. Why? How can i fix it? Any ideas?

nuklear
  • 11
  • 1
  • First of all: reduce your example: you dont need to confuse us with that string parsing and changing code. Just create a set of strings like 192.168.1.102 and see what `isReachable()` would return for that. In other words: focus on **one** problem at a time! When you are sure that java code thinks "reachable" - then have a look at the addresses your program creates, and why you get different results then. – GhostCat Jul 27 '17 at 15:31
  • While `ping` use `ICMP` documentation on `isReachable` state that it may use `TCP` connection to `echo` port (7). – talex Jul 27 '17 at 15:32
  • I went to google and all I searched on was "InetAddress.isReachable" and the second hit is "why does InetAddress.isReachable return false when I can ping the IP address"... Exactly what you're asking... – Rabbit Guy Jul 27 '17 at 15:34
  • I tried the code in that question, but the result is the same. Can i solve? – nuklear Jul 27 '17 at 15:39
  • Tried the code in the accepted answer? – Rabbit Guy Jul 27 '17 at 15:52
  • Please read the accepted answer as it will explain why this approach isn't working for you and some alternative possible solutions – Rabbit Guy Jul 27 '17 at 15:52

0 Answers0