5 Replies Latest reply: Mar 24, 2009 1:56 AM by 807588 RSS

    Multithreads and InetAddress

    807588
      My goal is to find out what ip addresses are reachable. I want to find out all ip address 192.168.1.1 ~ 192.168.1.255
      The timeout for each ip address is 2 seconds.

      In order to speed up the code I decided to execute each query in its own thread. That way it will only be 2 seconds to find out if all of them are reachable or not.

      I believe it is checking the 192.168.1.1 address correct. It prints out as -64 which is the two's complement. As you can see from the output below it prints out the results of the first one five times instead of all of them.

      Help please.
      import java.net.InetAddress;
      /**
       *
       * @author name
       */
      public class IpValidationImpl extends Thread {
          byte[] ipAddr;
          boolean validAddress;
      
          public IpValidationImpl(byte[] ip){
              ipAddr = ip;
              //System.out.println ((int)ip[0]);
          }
      
          public void run(){
              int myTimeout = 2000;
              //byte[] address = { 127, 0, 0, 1 };
              try {
                  InetAddress addr = InetAddress.getByAddress(ipAddr);
                  validAddress = (addr.isReachable( myTimeout ));
                  if (validAddress)
                      System.out.println ("Good: " + (int)ipAddr[0] + "." + (int)ipAddr[1] + "." + (int)ipAddr[2]+ "." + (int)ipAddr[3]);
              }
              catch (Exception e){
                  System.err.println("Error " + e);
              }
          }
      
          //public boolean isValid(){
            /// if (ip.get(i).isReachable(2000))
               //      System.out.println("Ip: " + i);
                // else
                  //   System.out.println("No Ip: " + i);
          //}
      }
      and this next part is part of my class.
       private void jButtonFindIpActionPerformed(java.awt.event.ActionEvent evt) {                                              
             byte[] address = { (byte)192, (byte)168, 1, 1 };
             byte[] ada = { 127, 0, 0, 1 };
             byte[] abc = { 127, 0, 0, 1 };
             List<IpValidationImpl> ip = new ArrayList<IpValidationImpl>();
             int j;
             for (int i = 0; i < 5; i++){
                j = i + 1;
                 abc[3] = (byte)j;
                 ip.add(new IpValidationImpl(address));
                 ip.get(i).start();
             }
      }
      output:

      Good: -64.-88.1.1
      Good: -64.-88.1.1
      Good: -64.-88.1.1
      Good: -64.-88.1.1
      Good: -64.-88.1.1

      Edited by: inspiron630 on Mar 23, 2009 10:34 PM

      Edited by: inspiron630 on Mar 23, 2009 10:36 PM
        • 1. Re: Multithreads and InetAddress
          EJP
          You're reusing the same byte array for all objects. Create a new one per object.

          But:
          I believe it is checking the 192.168.1.1 address correct. It prints out as -64 which is the two's complement
          Two's complement of what? Not of 192.168.1.1.
          • 2. Re: Multithreads and InetAddress
            807588
            thanks. it works now.
            The only querk is that 192.168.1.1 prints out as -64.-88.1.1 because I casted it as a byte. What is a clean way to get it back to 192.168.1.1 for the print out?
            • 3. Re: Multithreads and InetAddress
              EJP
              InetAddress.getByAddress(address).getHostAddress()
              • 4. Re: Multithreads and InetAddress
                807588
                awesome! last question.

                for some reason its not finding 192.168.1.5. I can successfully ping it but the program doesn't find it. It finds all other ip address on the network though, 192.168.1.1 and 192.168.1.4 and 192.168.1.6.

                is there more that is required for .isReachable then just ping able to ping the address?
                private void jButtonFindIpActionPerformed(java.awt.event.ActionEvent evt) {                                              
                       List<IpValidationImpl> ip = new ArrayList<IpValidationImpl>();
                       int j = 1;
                       for (int i = 0; i < 255; i++, j++){
                           byte[] address = { (byte)192, (byte)168, 1, (byte)j };
                           ip.add(new IpValidationImpl(address));
                           ip.get(i).start();
                       }
                }
                import java.net.InetAddress;
                /**
                 *
                 * @author name
                 */
                public class IpValidationImpl extends Thread {
                    byte[] ipAddr;
                    boolean validAddress;
                
                    public IpValidationImpl(byte[] ip){
                        ipAddr = ip;
                        //System.out.println ((int)ip[0]);
                    }
                
                    public void run(){
                        int myTimeout = 2000;
                        //byte[] address = { 127, 0, 0, 1 };
                        try {
                            InetAddress addr = InetAddress.getByAddress(ipAddr);
                            validAddress = (addr.isReachable( myTimeout ));
                            if (validAddress)
                                System.out.println (InetAddress.getByAddress(ipAddr).getHostAddress() );
                        }
                        catch (Exception e){
                            System.err.println("Error " + e);
                        }
                    }
                
                    //public boolean isValid(){
                      /// if (ip.get(i).isReachable(2000))
                         //      System.out.println("Ip: " + i);
                          // else
                            //   System.out.println("No Ip: " + i);
                    //}
                }
                • 5. Re: Multithreads and InetAddress
                  807588
                  i increased my timeout time to 3 seconds and it finds all of them.

                  thanks for all the help.