get IP addresses of computer available on a network? -java

后端 未结 7 2241
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 02:36

I am making a file sharing application which would look for computers which are running the application on the same network. So I would like my application to discover compu

7条回答
  •  被撕碎了的回忆
    2020-12-12 03:33

    I found the answer to this question..I looked around and found this code. It is not the best way to do it but it works..

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

提交回复
热议问题