Java Network Interface Get getNetworkInterface(String name)

Here you can find the source of getNetworkInterface(String name)

Description

Method that searches for and returns the network interface having the specified name.

License

Open Source License

Parameter

Parameter Description
name <code>String</code> referencing the name of the network interface to return (for example, typical values for this parameter might be, "eth0", "eth1", "hme01", "lo", etc., depending on how the underlying platform is configured).

Exception

Parameter Description
SocketException if there is an error in the underlyingI/O subsystem and/or protocol.
NullPointerException if <code>null</code> is input for<code>name</code>.

Return

an instance of NetworkInterface that represents the network interface corresponding to the given name, or null if there is no network interface with that name value.

Declaration

public static NetworkInterface getNetworkInterface(String name)
        throws SocketException 

Method Source Code

//package com.java2s;
/*//from   w ww.ja  va 2  s  .  c om

 Copyright (C) SYSTAP, LLC 2006-2008.  All rights reserved.

 Contact:
 SYSTAP, LLC
 4501 Tower Road
 Greensboro, NC 27410
 licenses@bigdata.com

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

import java.net.InetAddress;

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

public class Main {
    /**
     * Method that searches for and returns the network interface having
     * the specified <code>name</code>.
     *
     * @param name <code>String</code> referencing the name of the 
     *             network interface to return (for example, typical
     *             values for this parameter might be, "eth0", "eth1",
     *             "hme01", "lo", etc., depending on how the underlying
     *             platform is configured).
     *
     * @return an instance of <code>NetworkInterface</code> that represents
     *         the network interface corresponding to the given 
     *         <code>name</code>, or <code>null</code> if there is no
     *         network interface with that name value.
     *
     * @throws SocketException if there is an error in the underlying
     *         I/O subsystem and/or protocol.
     *
     * @throws NullPointerException if <code>null</code> is input for
     *         <code>name</code>.
     */
    public static NetworkInterface getNetworkInterface(String name)
            throws SocketException {
        NetworkInterface nic = NetworkInterface.getByName(name);
        if (nic == null) {
            // try by IP address
            InetAddress targetIp = null;
            try {
                targetIp = InetAddress.getByName(name);
                nic = NetworkInterface.getByInetAddress(targetIp);
            } catch (UnknownHostException uhe) {
                // ignore, return null
            }
        }
        return nic;
    }
}

Related

  1. getMac()
  2. getMacBytes()
  3. getNetworkErrorMessage(Throwable e)
  4. getNetworkInterface()
  5. getNetworkInterface()
  6. getNetworkInterfaceArray(String name)
  7. getNetworkinterfaceByName(String name)
  8. getNetworkInterfaceInfo(NetworkInterface pNif)
  9. getNetworkInterfaces( Predicate predicate)