Getting Your Own Device IP Address using Java
Last Updated :
08 Jun, 2022
An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. An IP address serves two principal functions: host or network interface identification and location addressing. There are two versions of internet protocol addresses which are named as follows:
An IPv4 Addresses consists of 32 bits which limit the address space to 4294967296 (232) possible unique addresses. IPv4 reserves some addresses for special purposes such as private networks (~18 million addresses) or multicast addresses (~270 million addresses). The rapid exhaustion of IPv4 address space prompted the Internet Engineering Task Force to explore new technologies to expand the addressing capability on the Internet. This new generation of Internet Protocol was eventually named Internet Protocol Version 6 (IPv6) in 1995. The address size was increased from 32 to 128 bits (16 octets), thus providing up to 2128 (approximately 3.403×1038) addresses.
Implementation:
Pseudo-code is as follows:
InetAddress myIP = InetAddress.getLocalHost();
System.out.println(myIP.getHostAddress());
Example
Java
import java.io.*;
import java.net.InetAddress;
public class GFG {
public static void main(String[] args)
{
try {
InetAddress myIP = InetAddress.getLocalHost();
System.out.println( "My IP Address is : " );
System.out.println(myIP.getHostAddress());
}
catch (Exception e) {
System.out.println( "Some Error Occurred" );
}
}
}
|
Output
My IP Address is:
127.0.0.1
Share your thoughts in the comments
Please Login to comment...