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