NetworkInterface.networkInterfaces() in Java

NetworkInterface.networkInterfaces(): This method is available in the java.net.NetworkInterface class of Java.

Syntax:

Stream<NetworkInterface> java.net.NetworkInterface.networkInterfaces() throws SocketException

This method returns a Stream of all the interfaces on this machine. The Stream contains at least one interface, possibly representing a loopback interface that only supports communication between entities on this machine.

Parameters: NA

Returns: a Stream of NetworkInterfaces found on this machine.

Throws:

1. SocketException - if an I/O error occurs, or if the platform does not have at least one configured network interface.

Approach

Java

package com.NetworkInterface;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.stream.Stream;

public class NetworkInterfacenetworkInterfaces {
    public static void main(String[] args)
throws SocketException {

        Stream<NetworkInterface> networkInterface = NetworkInterface.networkInterfaces();

        networkInterface.forEach(n -> System.out.println(n));

    }
}

Output:

name:lo (Software Loopback Interface 1) name:net0 (Microsoft 6to4 Adapter) name:net1 (WAN Miniport (L2TP)) name:net2 (WAN Miniport (SSTP)) name:net3 (Microsoft IP-HTTPS Platform Adapter) name:eth0 (WAN Miniport (Network Monitor)) name:net4 (WAN Miniport (IKEv2)) name:wlan0 (Microsoft Wi-Fi Direct Virtual Adapter) name:ppp0 (WAN Miniport (PPPOE)) name:wlan1 (Microsoft Wi-Fi Direct Virtual Adapter #2) name:eth1 (WAN Miniport (IPv6)) name:net5 (WAN Miniport (PPTP)) name:eth2 (Bluetooth Device (Personal Area Network)) name:eth3 (Intel(R) Ethernet Connection (3) I218-V) name:net6 (Microsoft Teredo Tunneling Adapter) name:wlan2 (Intel(R) Dual Band Wireless-AC 3160) name:eth4 (Cisco AnyConnect Secure Mobility Client Virtual Miniport Adapter for Windows x64) name:eth5 (Microsoft Kernel Debug Network Adapter) name:eth6 (WAN Miniport (IP)) name:eth7 (Intel(R) Ethernet Connection (3) I218-V-WFP Native MAC Layer LightWeight Filter-0000) name:eth8 (Intel(R) Ethernet Connection (3) I218-V-QoS Packet Scheduler-0000) name:eth9 (Intel(R) Ethernet Connection (3) I218-V-WFP 802.3 MAC Layer LightWeight Filter-0000) name:wlan3 (Intel(R) Dual Band Wireless-AC 3160-WFP Native MAC Layer LightWeight Filter-0000) name:wlan4 (Intel(R) Dual Band Wireless-AC 3160-Virtual WiFi Filter Driver-0000) name:wlan5 (Intel(R) Dual Band Wireless-AC 3160-Native WiFi Filter Driver-0000) name:wlan6 (Intel(R) Dual Band Wireless-AC 3160-QoS Packet Scheduler-0000) name:wlan7 (Intel(R) Dual Band Wireless-AC 3160-WFP 802.3 MAC Layer LightWeight Filter-0000) name:wlan8 (Microsoft Wi-Fi Direct Virtual Adapter-WFP Native MAC Layer LightWeight Filter-0000) name:wlan9 (Microsoft Wi-Fi Direct Virtual Adapter-Native WiFi Filter Driver-0000) name:wlan10 (Microsoft Wi-Fi Direct Virtual Adapter-QoS Packet Scheduler-0000) name:wlan11 (Microsoft Wi-Fi Direct Virtual Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000) name:eth10 (WAN Miniport (IP)-WFP Native MAC Layer LightWeight Filter-0000) name:eth11 (WAN Miniport (IP)-QoS Packet Scheduler-0000) name:eth12 (WAN Miniport (IPv6)-WFP Native MAC Layer LightWeight Filter-0000) name:eth13 (WAN Miniport (IPv6)-QoS Packet Scheduler-0000) name:eth14 (WAN Miniport (Network Monitor)-WFP Native MAC Layer LightWeight Filter-0000) name:eth15 (WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000) name:wlan12 (Microsoft Wi-Fi Direct Virtual Adapter #2-WFP Native MAC Layer LightWeight Filter-0000) name:wlan13 (Microsoft Wi-Fi Direct Virtual Adapter #2-Native WiFi Filter Driver-0000) name:wlan14 (Microsoft Wi-Fi Direct Virtual Adapter #2-QoS Packet Scheduler-0000) name:wlan15 (Microsoft Wi-Fi Direct Virtual Adapter #2-WFP 802.3 MAC Layer LightWeight Filter-0000)


Some other methods of NetworkInterface class

equals(Object)This method compares this object against the specified object.

NetworkInterface.getByIndex(int)This method gets a network interface given its index.

NetworkInterface.getByInetAddress(InetAddress)Convenience method to search for a network interface that has the specified Internet Protocol (IP) address bound to it.

NetworkInterface.getByName(String)This method searches for the network interface with the specified name.

getDisplayName()This method gets the display name of this network interface.

getHardwareAddress()This method returns the hardware address (usually MAC) of the interface if it has one and if it can be accessed given the current privileges.

getIndex()This method returns the index of this network interface.

getInetAddresses()This method gets an Enumeration with all or a subset of the InetAddresses bound to this network interface.

getInterfaceAddresses()This method gets a List of all or a subset of the InterfaceAddressesof this network interface.

getMTU()This method returns the Maximum Transmission Unit (MTU) of this interface.

getName()This method gets the name of this network interface.

NetworkInterface.getNetworkInterfaces()This method returns an Enumeration of all the interfaces on this machine.

getParent()This method returns the parent NetworkInterface of this interface if this is a sub-interface, or null if it is a physical(non-virtual) interface or has no parent.

getSubInterfaces()This method gets an Enumeration with all the subinterfaces (also known as virtual interfaces) attached to this network interface.

hashCode()This method returns a hash code value for the object.

inetAddresses()This method gets a Stream of all or a subset of the InetAddresses bound to this network interface.

isLoopback()This method returns whether a network interface is a loopback interface.

isPointToPoint()This method returns whether a network interface is a point-to-point interface.

isUp()This method returns whether a network interface is up and running.

isVirtual()This method returns whether this interface is a virtual interface.

NetworkInterface.networkInterfaces()This method returns a Stream of all the interfaces on this machine.

subInterfaces()This method gets a Stream of all subinterfaces attached to this network interface.

supportsMulticast()This method returns whether a network interface supports multicasting or not.

toString()This method returns a string representation of the object. In general, the toString method returns a string that"textually represents" this object.

No comments:

Post a Comment