Java Networking (Help for my project)

76 Views Asked by At

This code list all connected ip's on network (localhost) but I would like that the list of ip's depends to the interface name. Please help me!

Example:

Interfaces:

  1. eth1
  2. eth2

Enter the interface number:

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");
    }
    }

}
0

There are 0 best solutions below