Get IP Address in Java

Ads
 

Get IP Address in Java

In this example we will describe How to get ip address in java. An IP address is a numeric unique recognition number which is assigned to the devices participating in the network for communication.

In this example we will describe How to get ip address in java. An IP address is a numeric unique recognition number which is assigned to the devices participating in the network for communication.

Get IP Address in Java

In this example we will describe How to get ip address in java. An IP address is a numeric unique recognition number which is assigned to the devices participating in the network for communication.

In this example We have used for method:

  • InetAddress().
  • getLocalHost().
  • getHostAddress().

InetAddress():-The InetAddress is class represents an IP address.

GetLocalHost():-We have used static method getLocalHost() for getting the localhost. and return on IP address.

hetHostAddress():-In this method returns the information about the hostAddress for the current system.

Example

import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class IpAddress {
 
	public static void main(String[] args) {
 
	  InetAddress ip;
	  try {
 
		ip = InetAddress.getLocalHost();
		System.out.println("Current IP address : " + ip.getHostAddress());
 
	  } catch (UnknownHostException e) {
 
		e.printStackTrace();
 
	  }
 
	}
 }

Output

Download source code

Ads

Ads