5

I'm trying to write a code that will return my raspberry's IP when it's on the same network as my computer. The idea is for it to make a broadcast like Samba (Broadcast resolution is the closest to the original NetBIOS mechanism. Basically, a client looking for a service named Trillian will call out "Yo! Trillian! Where are you?", and wait for the machine with that name to answer with an IP address. Source: Samba team)

So here is the code:

public class GetIP {
    static String url; //global so I can access it after the threads are finished

    public class CheckIP extends Thread {
       private String url_test;

        public CheckIP(String url_t) {
            url_test = url_t;
        }

        public void run(){
            try {
                result = getHTML(this.url_test);  //result = the response from the GET request to this.url_test
            } catch (Exception e) {

            }

            if(result <is what I want>) {
                url = this.url_test  
                System.out.println("Flag 1");
                <I'd like to do something here, preferebly kill all other 
                threads that are trying to connect to an 'unserved' URL>
            }
        }
    }


    public static void main(String[] args) throws Exception{

        String ip_partial = <my computer's IP without the last part - ex: "192.168.0." , I'll hide the functions to make it short>;

        Thread myThreads[] = new Thread[254];
        for (int i = 1; i < 255; i++) {
            String url_test="http://"+ip_partial+i+":<port + endpoint>";
            GetIP getip = new GetIP ();
            myThreads[i] = new Thread(getip.new CheckIP(url_test));
            myThreads[i].start();
        }
        for (int i = 1; i < 254; i++) {
            System.out.println("Flag 2");
            myThreads[i].join(); //todo add catch exception
        }
    }   
}

I can see flag 1, and I did print the first 'for' so I know there are 254 threads being created, however I cannot see flag 2. It never shows, nomatter how long I wait. Any ideas why?

Laura Martins
  • 583
  • 2
  • 6
  • 15

1 Answers1

3

The problem with your code is java.lang.ArrayIndexOutOfBoundsException:

You are executing the loop till 254th index, whereas your array size itself is 254 which means the index present is 253 since java starts it's indexing from 0.

The first loop should also run the same no of iteration as your second loop.

for (int i = 1; i < 254; i++) {
                    /\
                    ||
                    ||
                    ||
      This should not be 255 else you'll get OutOfBounds Exception.
}
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • I thought that by starting at i=1 it would ignore position 0, and it wouldn't matter (because I'd not be filling or accessing the myThreads[0]) . I did that because on an IP adress (say X.X.X.Y), the Y = 0 and Y = 255 are never available (as far as I know), so as I'm completing the url with the int i I thought about the IP and not so much about the array. Such a stupid error, thank you for spotting it. :) – Laura Martins Nov 10 '17 at 06:14
  • In that case use the incremented value inside the loop instead. – Neeraj Jain Nov 10 '17 at 06:17
  • I did that now. Thank you, Neeraj. I'll be deleting this tomorrow, as it may mislead people with a different problem clicking on this link. But thank you :) – Laura Martins Nov 10 '17 at 06:20
  • you need not delete, it will help those people who might have similar confusion as you had. – Neeraj Jain Nov 10 '17 at 07:21