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(R) PRO/100 VE Network Connection - Packet Scheduler Miniport
InetAddress : /192.168.10.208
|
Download code

|