How to get the LAN IP of a client using Java?

前端 未结 5 981
攒了一身酷
攒了一身酷 2021-02-06 09:22

How can i get the LAN IP-address of a computer using Java? I want the IP-address which is connected to the router and the rest of the network.

I\'ve tried something like

5条回答
  •  一生所求
    2021-02-06 09:44

    try {
        InetAddress addr = InetAddress.getLocalHost();
    
        // Get IP Address
        byte[] ipAddr = addr.getAddress();
    
        // Get hostname
        String hostname = addr.getHostName();
    } catch (UnknownHostException e) {
    }
    

提交回复
热议问题