-3

I want the fastest way to find all the IP addresses of the PCs connected to the Lan

for(i=0;i<255;i++)

This kind of the code takes too much for searching and displaying . I want display ip addresses in the combobox. It takes 8 minutes to run and find ip addresses connected to the Lan.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package backupdropbox;

import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger;

/** * * @author Administrator */ public class Threading {

static InetAddress address;
static InetAddress localhost;
static Runnable r2 = new Runnable() {

    public void run() {



        try {
            localhost = InetAddress.getLocalHost();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
        }
        // this code assumes IPv4 is used
        byte[] ip = localhost.getAddress();
        for (int i = 1; i <= 50; i++) {
            ip[3] = (byte) i;
            try {
                address = InetAddress.getByAddress(ip);
            } catch (UnknownHostException ex) {
                Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
            }
            //   System.out.println("address"+address);
            String ip1 = address.getHostAddress();
            try {
                if (address.isReachable(2000)) {
                    try {
                        Socket ServerSok = new Socket(ip1, 3260);
                        System.out.println("Port in use: " + address);
                        ServerSok.close();
                    } catch (Exception e) {
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
            }

        }




    }
};
static Runnable r1 = new Runnable() {

    public void run() {



        try {
            localhost = InetAddress.getLocalHost();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
        }
        // this code assumes IPv4 is used
        byte[] ip = localhost.getAddress();
        for (int i = 51; i <= 100; i++) {
            ip[3] = (byte) i;
            try {
                address = InetAddress.getByAddress(ip);
            } catch (UnknownHostException ex) {
                Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
            }
            //   System.out.println("address"+address);
            String ip1 = address.getHostAddress();
            try {
                if (address.isReachable(2000)) {
                    try {
                        Socket ServerSok = new Socket(ip1, 3260);
                        System.out.println("Port in use: " + address);
                        ServerSok.close();
                    } catch (Exception e) {
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(Threading.class.getName()).log(Level.SEVERE, null, ex);
            }

        }




    }
};





public static void main(String args[]) {

    Thread thr1 = new Thread(r1);
    Thread thr2 = new Thread(r2);


    thr1.start();
    thr2.start();


}

}

here is my code after creating threads

2 Answers2

2

It sounds like you already have code that checks whether the given IP address is live.

Now, instead of running the code in a loop, you could either make it asynchronous, or partition the list of IP addresses to be checked across multiple threads.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

@Nilesh410451 I would be interested in seeing your code which does that. It might be useful one day.

As to a solution for your problem the answer is threading. You might use a SwingWorker to work in the background finding the new IPs then using invokeLater method from SwingUtilitis update the user GUI. Here you have not bad tutorial considering this type of issue.

Boro
  • 7,913
  • 4
  • 43
  • 85