Java examples for Network:Network Interface
Selects the default network interface
//package com.java2s; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class Main { public static void main(String[] argv) throws Exception { System.out.println(getDefaultNetworkInterface()); }/*ww w . j a v a 2s . c o m*/ /** * Selects the default network interface * @return network interface */ public static NetworkInterface getDefaultNetworkInterface() { try { Enumeration<NetworkInterface> networks = NetworkInterface .getNetworkInterfaces(); while (networks.hasMoreElements()) { NetworkInterface ni = networks.nextElement(); Enumeration<InetAddress> addresses = ni.getInetAddresses(); boolean hasAddress = false; while (addresses.hasMoreElements()) { InetAddress ia = addresses.nextElement(); if (!(ia.isAnyLocalAddress() || ia.isLinkLocalAddress() || ia.isLoopbackAddress() || ia.isSiteLocalAddress() || ni .isPointToPoint())) { if (!hasAddress) { return ni; } } } } } catch (Exception ex) { } return null; } }