Java Network Interface Get getAllLocalUsingNetworkInterface()

Here you can find the source of getAllLocalUsingNetworkInterface()

Description

Utility method that delegates to the methods of NetworkInterface to determine addresses for this machine.

License

Apache License

Exception

Parameter Description
UnknownHostException if there is a problem determining addresses

Return

all addresses found from the NetworkInterfaces

Declaration

private static InetAddress[] getAllLocalUsingNetworkInterface() throws UnknownHostException 

Method Source Code

//package com.java2s;
/**// ww w .  j av  a 2  s.c o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    /**
     * Utility method that delegates to the methods of NetworkInterface to
     * determine addresses for this machine.
     *
     * @return all addresses found from the NetworkInterfaces
     * @throws UnknownHostException if there is a problem determining addresses
     */
    private static InetAddress[] getAllLocalUsingNetworkInterface() throws UnknownHostException {
        try {
            List<InetAddress> addresses = new ArrayList<InetAddress>();
            Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
            while (e.hasMoreElements()) {
                NetworkInterface ni = e.nextElement();
                for (Enumeration<InetAddress> e2 = ni.getInetAddresses(); e2.hasMoreElements();) {
                    addresses.add(e2.nextElement());
                }
            }
            return addresses.toArray(new InetAddress[] {});
        } catch (SocketException ex) {
            throw new UnknownHostException("127.0.0.1");
        }
    }
}

Related

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