JAVA program to get IP adrress

Java program to get IP address

To write a java program to get the IP address, we first need to understand what an IP address is. The IP address is a unique sequence of four-octet numbers which is separated by full stops. We represent it in decimal form than binary form. we use it to identify a device available on the network. We also use it to get the address of a device by following the internet traffic to that device simply by not revealing the location. It helps to set the communication with other connected devices on the network. The IP address has two versions: IPV4 and IPV6.

IPV4

  • It is a 32 bit address length.
  • IPV4 supports manual configuration.
  • The security is totally dependent on the application.
  • It has a header of 20-6- bytes.
  • We denote this address numerical digits.

IPV6

  • It is 64 bit address length.
  • IPV6 supports auto configuration.
  • It has an inbuilt security feature IPSEC.
  • It has a header of 40 bytes.  
  • We denote this address by alpha-numerical digits.

Types of IP Address:

We have 4 types of IP addresses

  • Public: It is a type of IP address that can easily be accessed over the internet.
  • Private: It is used by the user in his own private space without exposing it to the internet.
  • Static: static is an IP address that doesn’t change. It remains the same once assigned to the user.
  • Dynamic: dynamic is an IP address that changes with time. It is cost-effective and generally, all the home networks tend to use the dynamic IP addresses.

Program to find IP Address of User

import java.net.InetAddress;
public class DeveloperHelps {
public static void main(String args[]) throws Exception{
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local Host Address is: "+addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name is: "+hostname);
}
}


The output of the above program in java will be:

Local Host Address is: 172.17.0.2
Local host name is: 0befd7fb6d92

In the above program, we have simply use getLocalAddress() method to fetch the IP address of the device. This address is bound to get the local address to which the system is concerned.

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!