Java Utililty Methods Network Interface Get

List of utility methods to do Network Interface Get

Description

The list of methods to do Network Interface Get are organized into topic(s).

Method

intgetLinkLocalScopeId()
get Link Local Scope Id
for (InetAddress a : getIPAddresses()) {
    if (a instanceof Inet6Address && a.isLinkLocalAddress()) {
        return ((Inet6Address) a).getScopeId();
throw new IOException("No IPv6 support on this device");
EnumerationgetLocalInterfaces()
Return the set of network interfaces for the local host.
return NetworkInterface.getNetworkInterfaces();
StringgetLocalMac()
get Local Mac
try {
    NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
    byte[] mac = network.getHardwareAddress();
    StringBuilder sb = new StringBuilder();
    for (int i = 0, ci = mac.length; i < ci; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < ci - 1) ? "-" : ""));
    return sb.toString();
...
NetworkInterfacegetLocalNetworkInterface()
get Local Network Interface
Enumeration<NetworkInterface> interfaces;
try {
    interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
    throw new RuntimeException("NetworkInterface not found", e);
while (interfaces.hasMoreElements()) {
    NetworkInterface networkInterface = interfaces.nextElement();
...
StringgetLoopbackInterface()
get Loopback Interface
String networkInterface = "";
try {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface resolvedNetworkInterface = interfaces.nextElement();
        if (resolvedNetworkInterface.isLoopback()) {
            networkInterface = resolvedNetworkInterface.getDisplayName();
            break;
...
StringgetLoopbackInterfaceName()
Get the loopback interface name.
try {
    for (NetworkInterface netIf : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        if (netIf == null) {
            continue;
        if (netIf.isLoopback()) {
            return netIf.getName();
} catch (SocketException ex) {
return null;
NetworkInterfacegetLoopbackNIF()
Get a loopback NIF.
Enumeration<NetworkInterface> nets;
try {
    nets = NetworkInterface.getNetworkInterfaces();
} catch (SocketException se) {
    return null;
while (nets.hasMoreElements()) {
    NetworkInterface net = nets.nextElement();
...
StringgetMac()
get Mac
StringBuilder builder = new StringBuilder();
try {
    Enumeration<NetworkInterface> el = NetworkInterface.getNetworkInterfaces();
    while (el.hasMoreElements()) {
        byte[] mac = el.nextElement().getHardwareAddress();
        StringBuilder subBuilder = new StringBuilder();
        if (mac == null)
            continue;
...
byte[]getMacBytes()
get Mac Bytes
try {
    InetAddress ia = InetAddress.getLocalHost();
    return NetworkInterface.getByInetAddress(ia).getHardwareAddress();
} catch (Exception e) {
    return null;
StringgetNetworkErrorMessage(Throwable e)
get Network Error Message
if ("InvalidProtocolBufferException".equals(e.getClass().getSimpleName())) {
    return "Your Internet provider may require you to log in via your web browser.";
if (e instanceof UnknownHostException) {
    return "You may not be connected to the Internet.";
} else {
    return e.getClass().getSimpleName() + ": " + e.getMessage();