java - Speed up finding devices connected to a Wifi network and get device name -
i'm writing java program show names , ip addresses of devices connected wifi network.
i've figured out ip address part. here's code :
public static void main(string[] args) throws ioexception { inetaddress localhost = inetaddress.getlocalhost(); // code assumes ipv4 used byte[] ip = localhost.getaddress(); (int = 1; <= 254; i++) { ip[3] = (byte) i; inetaddress address = inetaddress.getbyaddress(ip); if (address.isreachable(1000)) { // machine turned on , can pinged system.out.println(address + "is online"); } else if (!address.gethostaddress().equals(address.gethostname())) { // machine known in dns lookup system.out.println(address + "is in dns lookup"); } else { // host address , host name equal, meaning host name not resolved system.out.println(address + " not online"); } } }
this code works , shows ip addresses of connected devices.
there 2 problems i'm facing:
- i'm not able connected device's name. can ip address.
- this program works slow. takes 254 seconds complete.
so how display name of connected devices , there way speed program up?
any appreciated!
this program works slow. takes 254 seconds complete.
i think know why. inetaddress documentation:
public boolean isreachable(int timeout) throws ioexception
the timeout value, in milliseconds, indicates maximum amount of time try should take. if operation times out before getting answer, host deemed unreachable. negative value result in illegalargumentexception being thrown.
therein lies problem. if allocate second timeout
value, program take 254 seconds complete if hosts unreachable. try reducing it.
Comments
Post a Comment