Java Network Interface Get getDefaultInterface()

Here you can find the source of getDefaultInterface()

Description

Returns the system's default interface.

License

Open Source License

Exception

Parameter Description
SocketExceptionif an I/O error occurs while querying the interfaces.

Declaration

public static NetworkInterface getDefaultInterface() throws SocketException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.*;

import java.util.Enumeration;

public class Main {
    /**//w  ww.  j  av a  2  s . c  o m
     * Returns the system's default interface. Returns {@code null} if there are no
     * non-loopback interfaces in the UP state.
     * <p/>
     * The default interface is the first interface returned by the system that is UP
     * and is not a loop back interface. It does not imply that it represents the
     * default route.
     *
     * @throws SocketException  if an I/O error occurs while querying the interfaces.
     */
    public static NetworkInterface getDefaultInterface() throws SocketException {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface inter = e.nextElement();
            if (inter.isUp() && !inter.isLoopback())
                return inter;
        }
        return null;
    }
}

Related

  1. getAllLocalInterfaces()
  2. getAllLocalUsingNetworkInterface()
  3. getAvailableNetworkInterface()
  4. getBroadcast()
  5. getCustomMACFormat(NetworkInterface inte)
  6. getDefaultNetworkInterface()
  7. getFirstMACAdress()
  8. getFreeTunnelInterface()
  9. getGlobalI()