Java Network Interface Get getAvailableNetworkInterface()

Here you can find the source of getAvailableNetworkInterface()

Description

get Available Network Interface

License

Open Source License

Declaration

public static NetworkInterface getAvailableNetworkInterface() 

Method Source Code

//package com.java2s;
/*/*  w w w  . jav  a  2 s  .  c  o  m*/
 * Copyright Siemens AG, 2015. Part of the SW360 Portal Project.
 *
 * SPDX-License-Identifier: EPL-1.0
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.*;

public class Main {
    public static NetworkInterface getAvailableNetworkInterface() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                if (networkInterface.isLoopback())
                    continue;

                if (networkInterface.isUp())
                    return networkInterface;
            }
            return null;
        } catch (SocketException e) {
            return null;
        }
    }
}

Related

  1. getAllAvailableInterfaces()
  2. getAllInterfaces()
  3. getAllInterfaces()
  4. getAllLocalInterfaces()
  5. getAllLocalUsingNetworkInterface()
  6. getBroadcast()
  7. getCustomMACFormat(NetworkInterface inte)
  8. getDefaultInterface()
  9. getDefaultNetworkInterface()