hi,
how can I find the adress of all connected machines to the network,with InetAdress and thanks for any help

Dani AI

Generated

Short answer: you cannot reliably discover every host on a LAN from a single Java process unless a network device (DHCP/router/switch) cooperates. was right that active probes only find machines that respond; asked about InetAddress, so below are practical, implementable options and a small Java pattern to try.

A pragmatic Java approach is to combine an active scan (fast, parallel InetAddress.isReachable or short TCP connect attempts) with passive/local sources (ARP table, DHCP lease table or SNMP on the gateway). isReachable can detect hosts that respond to ICMP or an outgoing TCP probe depending on platform/permissions, but many hosts block ICMP and some OSes require elevated rights for raw ICMP. Use a thread pool to scan the subnet rather than serial checks.

Example (simple, parallel reachability probe):

import java.net.*;
import java.util.concurrent.*;

ExecutorService ex = Executors.newFixedThreadPool(50);

for (int i = 1; i < 255; i++) {
  final String host = "192.168.1." + i;
  ex.submit(() -> {
    try {
      InetAddress ia = InetAddress.getByName(host);
      if (ia.isReachable(500)) {
        System.out.println(host + " is reachable");
      }
    } catch (Exception ignored) {}
  });
}

ex.shutdown();
ex.awaitTermination(30, TimeUnit.SECONDS);

If more accuracy is needed, parse the OS ARP cache (arp -a or /proc/net/arp), query the router/DHCP lease table or use SNMP, or run a dedicated discovery tool like Nmap for deeper probes. Always respect network policies and obtain permission before scanning. For Java docs on InetAddress, see the Java API (https://docs.oracle.com/javase/8/docs/api/java/net/InetAddress.html).

Recommended Answers

All 2 Replies

Realistically I don't think you can. If machines on the network chose not to talk to you then you can't find them. You can use the "ping" protocol to find machines that are happy to respond to a "ping", but that's about as far as it goes.

... but if you have access to the DHCP/local DNS server for the network you may be able to get a list from the server's user interface somehow???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.