Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Network Computer Names

Tags:

java

I'm looking for a better way to get the computer names in my LAN network with Java. I have tried:

byte[] ip = {(byte)192,(byte)168,(byte)178,(byte)1};
    for(int i=1;i<255;i++)
    {
        ip[3] = (byte)i;
        try
        {
            InetAddress addr = InetAddress.getByAddress(ip);
            String s = addr.getHostName();
            System.out.println(s);
        }
        catch(UnknownHostException e)
        {
            System.out.println(e.getMessage());
        }
    }

... but it's too slow. Is there any other way?

I am on Windows.

Any ideas are appreciated.

like image 339
user1380282 Avatar asked Dec 26 '22 23:12

user1380282


1 Answers

You can increase speed by using multiple threads.

Have each thread execute one or more of the iterations of your 'try' block.

like image 183
Colin D Avatar answered Jan 12 '23 05:01

Colin D