How to Get localhost IP Address in Java
Using the java.net.InetAddress
, We can Get the localhost IP Address in Java. To get the localhost IP, We can Use the Code like
import java.net.InetAddress; import java.net.UnknownHostException; public class IPAddress { public static void main(String[] args) { try { InetAddress ip = InetAddress.getLocalHost(); System.out.println("IP is : " + ip.getHostAddress()); } catch (UnknownHostException ex) { ex.printStackTrace(); } } }
IP is : 192.168.1.5
The traditional IP Addresses (IPv4) uses a 32-bit number to represent an IP address, and it defines both network and host address. Due to IPv4 addresses running out, a new version of the IP protocol (IPv6) has been invented to offer virtually limitless number of unique addresses. An IP address is written in “dotted decimal” notation, which is 4 sets of numbers separated by period each set representing 8-bit number ranging from (0-255). An example of IPv4 address is 172.0.0.1