Home Java Java-get-example Get IP Address Example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Get IP Address Example

Advertisement
This example shows you ip address of your network interface.

Get IP Address Example

     

This example shows you ip address of your network interface.

Description of the code :

NetworkInterface.getNetworkInterfaces() : Network Interface class is used for get name, and a list of IP addresses assigned to this interface and getNetworkInterfaces method is used for get the list of all InterfaceAddresses of this network interface.

NetworkInterface.getDisplayName() : getDisplayName method is used for display the name of network interface.

NetworkInterface.getInetAddresses : getInetAddress method is used for display the inetaddress of network interface.

IpAddress.java

import java.net.*;
import java.util.*;

public class IpAddress {

  public static void main(String args[]) throws Exception {

  Enumeration<NetworkInterface> nets = 
  NetworkInterface.getNetworkInterfaces
();

  for (NetworkInterface netint : Collections.list(nets)) {
  System.out.println("\nDisplay name : " + netint.getDisplayName());

  Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();

  for (InetAddress inetAddress : Collections.list(inetAddresses)) {

  System.out.println("InetAddress : " + inetAddress);
  }
  }
  }
}

Output:
Display name : MS TCP Loopback interface
InetAddress : /127.0.0.1

Display name : Intel(RPRO/100 VE Network Connection - Packet Scheduler Miniport
InetAddress : /192.168.10.208

Download code
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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: October 23, 2008

Related Tutorials

Ask Questions?    Discuss: Get IP Address Example  

Post your Comment


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