Results 1 to 5 of 5
  1. #1
    stiln is offline Member
    Join Date
    Dec 2015
    Posts
    3
    Rep Power
    0

    Lightbulb How to get List of All computers in the same network

    Hey All,
    I am working on a project in which i have to create a java application on having the information of devices in the same network.
    I just want to find the IP address and Computer names.

    Here i've found some code online and do some editing. but, does this appropriate to be used to check on all connected devices in the same network? or are there better way to do it?


    Thanks & Regard
    Stiln D


    Java Code:
    package pinger;
    
    import java.io.IOException;
    
    import java.net.InetAddress;
     
    public class NetworkPing {
     
    	
    	public static void main(String[] args) throws IOException {
     
    		InetAddress localhost = InetAddress.getLocalHost();
    		// this code assumes IPv4 is used
    		byte[] ip = localhost.getAddress();
     
    		for (int i = 1; i <= 254; i++)
    		{
    			ip[3] = (byte)i;
    			InetAddress address = InetAddress.getByAddress(ip);
    		if (address.isReachable(1000))
    		{
    			System.out.println(address + " machine is turned on and can be pinged");
    		}
    		else if (!address.getHostAddress().equals(address.getHostName()))
    		{
    			System.out.println(address + " machine is known in a DNS lookup");
    		}
    		//else
    		//{
    		//	System.out.println(address + " the host address and host name are equal, meaning the host name could not be resolved");
    		//}
    		
    		
    		}
     //System.out.println("End of searching");
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to get List of All computers in the same network

    Please don't post the same question multiple times.
    I'll leave this one her, as it has a better title that "Hellp", and close the other one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    stiln is offline Member
    Join Date
    Dec 2015
    Posts
    3
    Rep Power
    0

    Default Re: How to get List of All computers in the same network

    Quote Originally Posted by Tolls View Post
    Please don't post the same question multiple times.
    I'll leave this one her, as it has a better title that "Hellp", and close the other one.
    oh okay, sorry

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    7

    Default Re: How to get List of All computers in the same network

    If am not wrong I believe that this is not going to work.. Because each machine may have different firewall rules so in some cases you will not be able to check if a computer is reachable or not cause a ping message could be restricted by firewall.
    Secondly the 'address.isReachable(1000)' command is not safe enough cause a value like 1000 for a computer 'X' may be enough and for some others not but this depends on Internet speed i guess.
    I believe the safest way to detect if a computer is reachable on a LAN network is to use the PING command through java Runtime.getRuntime().exec(command); and check the results (package lost etc).

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to get List of All computers in the same network

    Okay, here is the scoop. First, the standard Java IP package is not very good because:

    1. You can't send raw IP packets which you construct.
    2. You can't send homegrown ICMP packets via the normal Java packet protocols. (At least based on my previous perusal
    it didn't appear you could).

    So you could use a third party package that lets you construct and process ICMP echo and echo reply packets to do a ping in java. But then you may
    have the afforementioned firewall problem. So go ahead and try and ping your hosts as best you can. Then check your local ARP table
    to see what mappings of IP to mac addresses exist. These are created dynamically to resolve LAN addresses.

    Since the ARP table can last longer than a host, you may want to clear it first and then do the pings. You might try using a broadcast ping. I used to do this on Sun workstations when I worked as a network engineer. I would then get back numerous replies. Note that the hosts must respond even with a firewall because the packet may be legitimate (they have to see it to reject it).

    Then check you newly created ARP table. In an MSDOS window, try the arp command to see the table. Also ARP stands for Address Resolution protocol. Also note that I do not know how to retrieve a local arp table via Java. You may have to execute an external command to do this.

    EDIT: I use Windows hence the MSDOS statement. For Unix and its variants, any command window should suffice.

    Regards,
    Jim
    Last edited by jim829; 12-16-2015 at 06:27 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Helpp
    By stiln in forum Networking
    Replies: 0
    Last Post: 12-15-2015, 09:48 AM
  2. Help needed!!!!! choosing network computers
    By apoorv in forum Networking
    Replies: 6
    Last Post: 03-22-2011, 06:33 PM
  3. How do i get a list of SQL Servers from network?
    By flyto9 in forum Networking
    Replies: 0
    Last Post: 03-16-2011, 06:13 PM
  4. How to list all computer in a network?
    By CaptnCAPSLOCK in forum Networking
    Replies: 1
    Last Post: 01-24-2011, 02:12 PM
  5. Sockets for computers in wireless network
    By etrynus in forum Networking
    Replies: 2
    Last Post: 12-03-2008, 12:39 AM

Posting Permissions

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