Results 1 to 12 of 12

Thread: Get devices on LAN

  1. #1
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default Get devices on LAN

    I'm looking for a slicker way to access all of the online devices on a network using Java. I've currently got a method which displays all device names which have an open ServerSocket of a given port. The main application, shown below, then goes through all the devices on the subnet, and checks for this open SS. If it connects, it adds to the list of available devices.

    java Code:
    public class Hosts {

        public static void main(String[] args){
            //extract subnet from local IP
            String[] localIP = null;
            try {
                localIP = InetAddress.getLocalHost().getHostAddress().split("\\.");
            } catch (UnknownHostException e) {}
            String subnet = "";
            for (int i = 0; i < 3; i++)
                subnet += localIP[i] + ".";
           
            //display all available hosts with given port open
            InetAddress[] available = getNetworkAddresses(subnet);
            for (InetAddress addr : available)
                System.out.println(addr.getHostName().split("\\.")[0]);
        }
       
        public static InetAddress[] getNetworkAddresses(String subnet){
            final int PORT = 7777;
            final int TIMEOUT = 20;
           
            ArrayList<InetAddress> result = new ArrayList<InetAddress>();
           
            //loops through all IPs on the subnet, searching for connecting socket
            for (int i = 1; i < 255; i++){
                try {
                InetAddress host = InetAddress.getByName(subnet + i);
                Socket s = new Socket();
                s.connect(new InetSocketAddress(host, PORT), TIMEOUT);
                s.close();
               
                //successful connect if exception not thrown
                result.add(host);
                } catch (SocketTimeoutException e){
                    //if connection, next iteration
                    continue;
                } catch (Exception e){
                    e.printStackTrace();
                    System.exit(1);
                }
            }
            return result.toArray(new InetAddress[result.size()]);
        }
    }

    The issue with this is that a) it requires each device to be listening (this isn't necessarily a bad thing) and b) the timeout on each socket call causes the system to take a while (I know I could use threads, but this isn't particularly slick).

    Is there a more effective way to view all available devices? I've try pinging each address but this is both slow, and doesn't actually yield results

  2. #2
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Have you tried INetAddress.getAllByName(null)?

  3. #3
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    Async methods?

  4. #4
    Join Date
    Feb 2007
    Location
    Colorado, USA
    Posts
    3,716
    Mentioned
    51 Post(s)
    Quoted
    624 Post(s)

    Default

    You can check how nmap does it, it's GPL does pretty much everything you can ask for as far as network scanning
    The only true authority stems from knowledge, not from position.

    You can contact me via matrix protocol: @grats:grats.win or you can email me at the same domain, any user/email address.

  5. #5
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by tls View Post
    Have you tried INetAddress.getAllByName(null)?
    This just seems to return the local machine (i.e. localhost).

    Quote Originally Posted by Kasi View Post
    Async methods?
    I considered this, but threading seems to get the job done reasonable quickly, I was just hoping for a slightly more slick method, which wouldn't be so dependant on timeouts if there was a slow network speed.

    Quote Originally Posted by grats View Post
    You can check how nmap does it, it's GPL does pretty much everything you can ask for as far as network scanning
    I'll have a look into it, thanks!

  6. #6
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    A better test than connecting by socket would be InetAddress.isReachable(timeout)

  7. #7
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    @Richard; Pretty sure async methods are the way to do stuff like this. You could even call it a convention in Android i guess. I can't think of any other way right now.

  8. #8
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by tls View Post
    A better test than connecting by socket would be InetAddress.isReachable(timeout)
    isReachable doesn't seem to work, while pinging through cmd does seem to. Not sure why

    Quote Originally Posted by Kasi View Post
    @Richard; Pretty sure async methods are the way to do stuff like this. You could even call it a convention in Android i guess. I can't think of any other way right now.
    Could you give me an example? Honestly not familiar with async side of concurrency

  9. #9
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    @Richard; Google Async / Future tasks Java. Should be able to find some decent guides that would help you a lot more than i could.

    Edit: http://java.dzone.com/articles/javautilconcurrentfuture
    Last edited by Kasi; 04-14-2015 at 01:00 AM.

  10. #10
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by Kasi View Post
    @Richard; Google Async / Future tasks Java. Should be able to find some decent guides that would help you a lot more than i could.

    Edit: http://java.dzone.com/articles/javautilconcurrentfuture
    Thanks. I actually found a better way to perform it (still making use of async methods), but doesn't require the use of an open socket.

    Thanks all for the help

  11. #11
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    Thanks. I actually found a better way to perform it (still making use of async methods), but doesn't require the use of an open socket.

    Thanks all for the help
    Sharing is caring.

  12. #12
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    Thanks. I actually found a better way to perform it (still making use of async methods), but doesn't require the use of an open socket.

    Thanks all for the help
    No problem.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •