Here you can find the source of getNonLoopbackNetworkInterface()
private static NetworkInterface getNonLoopbackNetworkInterface() throws SocketException
//package com.java2s; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class Main { private static NetworkInterface getNonLoopbackNetworkInterface() throws SocketException { // We need to iterate over all NIC's machine has because stupid ubuntu does not assign // MAC address to default loopback interface... Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces(); while (b.hasMoreElements()) { NetworkInterface networkInterface = b.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress address = inetAddresses.nextElement(); if (!address.isLoopbackAddress()) return networkInterface; }//w ww.j av a 2s.c o m } // Means we haven't found any non loopback interfaces. Bummer, return empty handed. return null; } }