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.

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

Advertisements

 

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Get IP Address in Java

Posted on: July 18, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Get IP Address in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads