Java Network Interface Get getLinkLocalScopeId()

Here you can find the source of getLinkLocalScopeId()

Description

get Link Local Scope Id

License

Apache License

Declaration

public static int getLinkLocalScopeId() throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static int getLinkLocalScopeId() throws IOException {
        for (InetAddress a : getIPAddresses()) {
            if (a instanceof Inet6Address && a.isLinkLocalAddress()) {
                return ((Inet6Address) a).getScopeId();
            }/*from   w w w  .j  av  a2s  . c  om*/
        }
        throw new IOException("No IPv6 support on this device");
    }

    /**
     * List all IP addresses of the device, except loopback addresses.
     * 
     * @return a List of IP addresses
     * @throws IOException
     */
    public static List<InetAddress> getIPAddresses() throws IOException {
        List<InetAddress> addresses = new ArrayList<InetAddress>();
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress a : addrs) {
                if (a.isLoopbackAddress()) {
                    continue;
                }
                addresses.add(a);
            }
        }
        return addresses;
    }
}

Related

  1. getInterfaces()
  2. getInterfaces()
  3. getInterfaces()
  4. getInterfaces(final String interfaceName)
  5. getKey()
  6. getLocalInterfaces()
  7. getLocalMac()
  8. getLoopbackInterface()
  9. getLoopbackInterfaceName()