Example usage for java.net NetworkInterface getMTU

List of usage examples for java.net NetworkInterface getMTU

Introduction

In this page you can find the example usage for java.net NetworkInterface getMTU.

Prototype

public int getMTU() throws SocketException 

Source Link

Document

Returns the Maximum Transmission Unit (MTU) of this interface.

Usage

From source file:com.frostwire.util.VPNs.java

public static void printNetworkInterfaces() {
    try {//from w ww .j  a v  a  2 s  .  c  o m
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface iface = networkInterfaces.nextElement();
            System.out.println(iface.getIndex() + ":" + iface.getDisplayName() + ":" + "virtual="
                    + iface.isVirtual() + ":" + "mtu=" + iface.getMTU() + ":mac="
                    + (iface.getHardwareAddress() != null
                            ? "0x" + ByteUtils.encodeHex(iface.getHardwareAddress())
                            : "n/a"));
        }
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static void printParameter(NetworkInterface ni) throws SocketException {
    System.out.println(" Name = " + ni.getName());
    System.out.println(" Display Name = " + ni.getDisplayName());
    System.out.println(" Is up = " + ni.isUp());
    System.out.println(" Support multicast = " + ni.supportsMulticast());
    System.out.println(" Is loopback = " + ni.isLoopback());
    System.out.println(" Is virtual = " + ni.isVirtual());
    System.out.println(" Is point to point = " + ni.isPointToPoint());
    System.out.println(" Hardware address = " + ni.getHardwareAddress());
    System.out.println(" MTU = " + ni.getMTU());

    System.out.println("\nList of Interface Addresses:");
    List<InterfaceAddress> list = ni.getInterfaceAddresses();
    Iterator<InterfaceAddress> it = list.iterator();

    while (it.hasNext()) {
        InterfaceAddress ia = it.next();
        System.out.println(" Address = " + ia.getAddress());
        System.out.println(" Broadcast = " + ia.getBroadcast());
        System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength());
        System.out.println("");
    }/*from  w ww .j  a  v a  2 s. c o  m*/
}

From source file:Main.java

private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    System.out.printf("Display name: %s%n", netint.getDisplayName());
    System.out.printf("Name: %s%n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        System.out.printf("InetAddress: %s%n", inetAddress);
    }// w ww  .  j  a  v  a 2  s  .com

    System.out.printf("Parent: %s%n", netint.getParent());
    System.out.printf("Up? %s%n", netint.isUp());
    System.out.printf("Loopback? %s%n", netint.isLoopback());
    System.out.printf("PointToPoint? %s%n", netint.isPointToPoint());
    System.out.printf("Supports multicast? %s%n", netint.isVirtual());
    System.out.printf("Virtual? %s%n", netint.isVirtual());
    System.out.printf("Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress()));
    System.out.printf("MTU: %s%n", netint.getMTU());

    List<InterfaceAddress> interfaceAddresses = netint.getInterfaceAddresses();
    for (InterfaceAddress addr : interfaceAddresses) {
        System.out.printf("InterfaceAddress: %s%n", addr.getAddress());
    }
    System.out.printf("%n");
    Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces();
    for (NetworkInterface networkInterface : Collections.list(subInterfaces)) {
        System.out.printf("%nSubInterface%n");
        displayInterfaceInformation(networkInterface);
    }
    System.out.printf("%n");
}

From source file:com.adamkruger.myipaddressinfo.NetworkInfoFragment.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static List<NetworkInterfaceInfo> getNetworkInterfaceInfos() {
    List<NetworkInterfaceInfo> networkInterfaceInfos = new ArrayList<NetworkInterfaceInfo>();
    try {/*from  w w w.  j  a va  2 s  .c o m*/
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface networkInterface : interfaces) {
            NetworkInterfaceInfo networkInterfaceInfo = new NetworkInterfaceInfo();
            networkInterfaceInfo.name = networkInterface.getDisplayName();
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
                byte[] MAC = networkInterface.getHardwareAddress();
                if (MAC != null) {
                    StringBuilder stringBuilder = new StringBuilder(18);
                    for (byte b : MAC) {
                        if (stringBuilder.length() > 0) {
                            stringBuilder.append(':');
                        }
                        stringBuilder.append(String.format("%02x", b));
                    }
                    networkInterfaceInfo.MAC = stringBuilder.toString();
                }

                networkInterfaceInfo.MTU = networkInterface.getMTU();
            }
            List<InetAddress> addresses = Collections.list(networkInterface.getInetAddresses());
            for (InetAddress address : addresses) {
                if (!address.isLoopbackAddress()) {
                    networkInterfaceInfo.ipAddresses.add(InetAddressToString(address));
                }
            }
            if (networkInterfaceInfo.ipAddresses.size() > 0) {
                networkInterfaceInfos.add(networkInterfaceInfo);
            }
        }
    } catch (SocketException e) {
    }

    return networkInterfaceInfos;
}

From source file:fr.inria.ucn.collectors.NetworkStateCollector.java

private JSONArray getIfconfig(Map<String, JSONObject> stats) throws JSONException {
    JSONArray ifaces = new JSONArray();

    // make sure the stats is read
    networkStats();/* w  w w  .  j a  v  a 2s  . co m*/

    Enumeration<NetworkInterface> en = null;
    try {
        en = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        Log.d(Constants.LOGTAG, "failed to list interfaces", e);
    }

    if (en != null) {
        while (en.hasMoreElements()) {
            NetworkInterface intf = en.nextElement();

            JSONObject iface = new JSONObject();
            iface.put("display_name", intf.getDisplayName());
            iface.put("name", intf.getName());
            iface.put("is_virtual", intf.isVirtual());
            iface.put("stats", stats.get(intf.getName()));

            try {
                iface.put("mtu", intf.getMTU());
                iface.put("is_loopback", intf.isLoopback());
                iface.put("is_ptop", intf.isPointToPoint());
                iface.put("is_up", intf.isUp());
            } catch (SocketException e) {
                Log.d(Constants.LOGTAG, "failed to read interface data", e);
            }

            JSONArray ips = new JSONArray();
            List<InterfaceAddress> ilist = intf.getInterfaceAddresses();
            for (InterfaceAddress ia : ilist) {
                ips.put(ia.getAddress().getHostAddress());
            }
            iface.put("addresses", ips);

            ifaces.put(iface);
        }
    } else {
        for (String name : stats.keySet()) {
            JSONObject iface = new JSONObject();
            iface.put("name", name);
            iface.put("stats", stats.get(name));
            ifaces.put(iface);
        }
    }

    return ifaces;
}

From source file:org.eclipse.smarthome.binding.lifx.internal.LifxLightDiscovery.java

@Override
protected void activate(Map<String, Object> configProperties) {
    super.activate(configProperties);

    broadcastAddresses = new ArrayList<InetSocketAddress>();
    interfaceAddresses = new ArrayList<InetAddress>();

    Enumeration<NetworkInterface> networkInterfaces = null;
    try {//w ww  . j  a  v  a  2 s .  c  o m
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        logger.debug("An exception occurred while discovering LIFX lights : '{}'", e.getMessage());
    }
    if (networkInterfaces != null) {
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface iface = networkInterfaces.nextElement();
            try {
                if (iface.isUp() && !iface.isLoopback()) {
                    for (InterfaceAddress ifaceAddr : iface.getInterfaceAddresses()) {
                        if (ifaceAddr.getAddress() instanceof Inet4Address) {
                            logger.debug("Adding '{}' as interface address with MTU {}", ifaceAddr.getAddress(),
                                    iface.getMTU());
                            if (iface.getMTU() > bufferSize) {
                                bufferSize = iface.getMTU();
                            }
                            interfaceAddresses.add(ifaceAddr.getAddress());
                            if (ifaceAddr.getBroadcast() != null) {
                                logger.debug("Adding '{}' as broadcast address", ifaceAddr.getBroadcast());
                                broadcastAddresses
                                        .add(new InetSocketAddress(ifaceAddr.getBroadcast(), BROADCAST_PORT));
                            }
                        }
                    }
                }
            } catch (SocketException e) {
                logger.debug("An exception occurred while discovering LIFX lights : '{}'", e.getMessage());
            }
        }
    }
}