Get a List of IP Connected in Same Network (Subnet) using Java
Networking is an essential concept in modern computing, forming the backbone of how devices communicate. In a local network, devices are often grouped into subnets for efficient communication and management. Being able to discover and list all devices within a subnet can be useful for network administrators, developers building distributed systems, or anyone interested in understanding network behavior. Let us delve into understanding how Java can handle IPs in the same subnet.
1. Understanding IP Addresses and Subnets
An IP Address is a unique identifier for a device on a network, enabling devices to locate and communicate with each other. IP addresses can be divided into two main types:
- IPv4: A 32-bit address represented in dotted decimal format (e.g., 192.168.1.10).
- IPv6: A 128-bit address represented in hexadecimal format, designed to address the limitations of IPv4.
1.1 What is a Subnet?
A subnet (short for “subnetwork”) is a logical subdivision of an IP network. Subnets allow better organization and security within a network by grouping IP addresses based on specific criteria. The size of a subnet is determined by the subnet mask.
1.2 Subnet Mask
A subnet mask defines which portion of an IP address represents the network and which part identifies individual devices. For example:
- IP Address: 192.168.1.10
- Subnet Mask: 255.255.255.0
Here, the first three octets (192.168.1
) represent the network, and the last octet (.10
) identifies the device. This subnet can have up to 254 devices (192.168.1.1 to 192.168.1.254).
1.3 CIDR Notation
Classless Inter-Domain Routing (CIDR) notation provides a concise way to define subnets. For example, 192.168.1.0/24
represents a subnet where the first 24 bits are for the network, leaving 8 bits for devices. This is equivalent to a subnet mask of 255.255.255.0
.
2. Using Java’s InetAddress Class
Java’s InetAddress
class provides methods to work with IP addresses and hostnames. It allows developers to query and verify network connectivity programmatically. Below is a simple implementation to scan and list active IP addresses in a subnet using InetAddress
:
import java.net.InetAddress; public class SubnetScanner { public static void main(String[] args) { String subnet = "192.168.1."; int timeout = 100; // in milliseconds for (int i = 1; i < 255; i++) { String host = subnet + i; try { InetAddress inet = InetAddress.getByName(host); if (inet.isReachable(timeout)) { System.out.println("Active IP: " + host); } } catch (Exception e) { System.err.println("Error checking IP: " + host); } } } }
2.1 Code explanation and output
The program iterates through all possible host addresses in the subnet 192.168.1.x
. For each address, it checks if the host is reachable within a specified timeout using the isReachable()
method. If the host is reachable, it prints the active IP address.
The output of the program:
Active IP: 192.168.1.2 Active IP: 192.168.1.10 Active IP: 192.168.1.15
3. Advanced Subnet Handling with Apache Commons Net Library
While Java’s built-in libraries provide basic networking functionality, handling complex subnet calculations can be tedious. The The Apache Commons Net Library simplifies this process by providing a dedicated class for subnet manipulation: SubnetUtils
. The library offers features like:
- Automatic calculation of all IPs within a subnet.
- Validation of IPs against a subnet.
- Support for CIDR notation.
3.1 Adding Jar dependency
Add the library to the code by adding the following dependency in your pom.xml
for Maven:
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>jar_version</version> </dependency>
3.1 Code example
Below is a simple implementation:
import org.apache.commons.net.util.SubnetUtils; public class SubnetScannerWithCommons { public static void main(String[] args) { String subnet = "192.168.1.0/24"; SubnetUtils utils = new SubnetUtils(subnet); String[] allIps = utils.getInfo().getAllAddresses(); for (String ip : allIps) { System.out.println("IP: " + ip); } } }
3.1.1 Code explanation and output
The program uses SubnetUtils
to calculate all possible IPs in the subnet defined by the CIDR range 192.168.1.0/24
. The method getAllAddresses()
returns an array of all IPs, which are then printed to the console.
The output of the program:
Active IP: 192.168.1.2 Active IP: 192.168.1.10 Active IP: 192.168.1.15
4. Conclusion
In this article, we explored two methods to list IPs in the same subnet using Java. The first approach uses the InetAddress
class for basic reachability testing, while the second leverages the Apache Commons Net library for advanced subnet manipulation. These techniques can be used for network diagnostics, monitoring, or building tools that require subnet awareness. By combining core Java features with external libraries, you can create efficient and scalable networking applications.