Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of IP connected in same network (subnet) using Java

How do I get list of IP addresses for devices connected to my same subnet using Java?

like image 223
jmj Avatar asked Jul 27 '10 16:07

jmj


People also ask

How do I list IP addresses on a subnet?

On Windows or macOS type ipconfig or on Linux type ifconfig. Press return. Note down the subnet mask, the default gateway, and your own computer's IPv4 address. Enter the command arp -a to get a list of all other IP addresses active on your network.

How can I see what IP addresses are on the same network?

Determine if a phone is on the same subnet as a computer connected to the network. Using the phone's menu, navigate to Status > TCIP/IP parameters (may differ slightly from phone to phone—please consult manufacturer's documentation). Write down both the phone's IP address and subnet mask.

How do I find the IP address and names of all devices on my local network android?

To check IP address of the local network on the Android device: Go to Settings → Network & internet on the tablet and select Wi-Fi. Tap the name of active network and expand the Advanced section. Find the Network details field with the local IP address.


2 Answers

this should work when the hosts on your network react to ICMP packages (ping) (>JDK 5):

public void checkHosts(String subnet){    int timeout=1000;    for (int i=1;i<255;i++){        String host=subnet + "." + i;        if (InetAddress.getByName(host).isReachable(timeout)){            System.out.println(host + " is reachable");        }    } } 

invoke the method for a subnet (192.168.0.1-254) like this:

checkHosts("192.168.0"); 

didnt test it but should work kinda like this. Obviously this only checks the 254 hosts in the last byte of the ip address...

check:

http://download-llnw.oracle.com/javase/6/docs/api/java/net/InetAddress.html#isReachable%28int%29 http://blog.taragana.com/index.php/archive/how-to-do-icmp-ping-in-java-jdk-15-and-above/

hope that helped

like image 161
fasseg Avatar answered Sep 23 '22 16:09

fasseg


To list the hosts connected in a LAN you will need to ping all the available IP addresses on the subnet. But a ping message could be restricted by firewall thus safer way could be open a socket to each IP address in the LAN's IP address range.

like image 22
Suhas Phartale Avatar answered Sep 24 '22 16:09

Suhas Phartale