NetworkInterface

In this chapter you will learn:

  1. How to use NetworkInterface class
  2. How to get MAC address

Using NetworkInterface

This class represents a Network Interface made up of a name, and a list of IP addresses assigned to this interface.

It is used to identify the local interface on which a multicast group is joined.

The following code outputs the information bounded to the NetworkInterface.

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
/*ja va 2s. c o  m*/
public class Main {
  public static void main(String args[]) throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
      displayInterfaceInformation(netint);
    }
  }

  private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    System.out.printf("Display name: %s%n", netint.getDisplayName());
    System.out.printf("Name: %s%n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
      System.out.printf("InetAddress: %s%n", inetAddress);
    }

    System.out.printf("Parent: %s%n", netint.getParent());
    System.out.printf("Up? %s%n", netint.isUp());
    System.out.printf("Loopback? %s%n", netint.isLoopback());
    System.out.printf("PointToPoint? %s%n", netint.isPointToPoint());
    System.out.printf("Supports multicast? %s%n", netint.isVirtual());
    System.out.printf("Virtual? %s%n", netint.isVirtual());
    System.out.printf("Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress()));
    System.out.printf("MTU: %s%n", netint.getMTU());

    List<InterfaceAddress> interfaceAddresses = netint.getInterfaceAddresses();
    for (InterfaceAddress addr : interfaceAddresses) {
      System.out.printf("InterfaceAddress: %s%n", addr.getAddress());
    }
    System.out.printf("%n");
    Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces();
    for (NetworkInterface networkInterface : Collections.list(subInterfaces)) {
      System.out.printf("%nSubInterface%n");
      displayInterfaceInformation(networkInterface);
    }
    System.out.printf("%n");
  }
}

Get MAC address

java.net.NetworkInterface.getHardwareAddress() gets MAC address of a host.

import java.net.InetAddress;
import java.net.NetworkInterface;
//  j a v a  2s .c o  m
public class Main {
  public static void main(String[] args) throws Exception {
    InetAddress address = InetAddress.getLocalHost();

    NetworkInterface ni = NetworkInterface.getByInetAddress(address);
    byte[] mac = ni.getHardwareAddress();

    for (int i = 0; i < mac.length; i++) {
      System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
    }
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is socket
Home » Java Tutorial » URL URI
URL
URL Creation
URL for jar file
URL Components
Convert file path to URL
URL Relative
URL Protocol
Read from URL
Compare URL
URLConnection
HTTP Header
URLConnection Post
Cookie
URLConnection Read
HttpURLConnection
HttpURLConnection Properties
HttpURLConnection proxy
HttpURLConnection Authenticator
HTTPS
JarURLConnection
Encode a URL
Decode a URL
URI
URI Normalization
URI Resolution
URI Relativization
Convert URI to URL
IP Address
IP Ping
NetworkInterface