Here you can find the source of getDefaultInterface()
Parameter | Description |
---|---|
SocketException | if an I/O error occurs while querying the interfaces. |
public static NetworkInterface getDefaultInterface() throws SocketException
//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; } }