265. Exploring network interfaces
In Java, a network interface is represented by the NetworkInterface
API. Basically, a network interface is identified by a name and a list of IPs that are assigned to it. Via this information, we can associate a network interface with different network tasks such as a multicast group.
The following snippet of code lists all network interfaces that are available on your machine:
public class Main {
public static void main(String[] args)
throws SocketException {
Enumeration allNetworkInterfaces
= NetworkInterface.getNetworkInterfaces();
while (allNetworkInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface)
allNetworkInterfaces.nextElement();
System.out.println("\nDisplay Name: "
+ ni.getDisplayName());
System.out.println(ni.getDisplayName()
+ " is up and running ? " + ni.isUp());
System.out.println(ni.getDisplayName()
...