Home Java Beginners Get IP Address in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Get IP Address in Java

Advertisement
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

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 18, 2013

Ask Questions?    Discuss: Get IP Address in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com