NetworkInterface Overview - Java Network

Java examples for Network:Network Interface

Introduction

NetworkInterface class represents a network interface.

NetworkInterface is made up of a name and a list of IP addresses assigned to this interface.

NetworkInterface is used to get the local interface to which a multicast group is joined.

The following code gets information about all the network interfaces.

Demo Code

import java.net.InetAddress;  
import java.net.NetworkInterface;  
import java.util.Enumeration;  
  
public class Main {  
  
 public static void main(String argv[]) throws Exception {  
  
  Enumeration enumInterfaces = NetworkInterface.getNetworkInterfaces();  
  while (enumInterfaces.hasMoreElements()) {  
     NetworkInterface net = (NetworkInterface) enumInterfaces.nextElement();  
     System.out.println("Network Interface Display Name: " + net.getDisplayName());  
     System.out.println(net.getDisplayName() + " is up and running ?" + net.isUp());  
     System.out.println(net.getDisplayName()+" Supports Multicast: "+net.supportsMulticast());  
     System.out.println(net.getDisplayName() + " Name: " + net.getName());  
     System.out.println(net.getDisplayName() + " Is Virtual:  " + net.isVirtual());  
     System.out.println("IP addresses:");  
     Enumeration enumIP = net.getInetAddresses();  
     while (enumIP.hasMoreElements()) {  
        InetAddress ip = (InetAddress) enumIP.nextElement();  
        System.out.println("IP address:" + ip);  
     }  /*from  w  w w.  j  a  v a 2 s. c o  m*/
  }  
 }  
}

Result


Related Tutorials