Java Tutorial/Network/NetworkInterface

Материал из Java эксперт
Версия от 05:03, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

An updated version of the ListNets program that uses the NetworkInterface enhancements

import java.io.Console;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
public class Main {
  public static void main(String args[]) throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
      displayInterfaceInformation(netint);
    }
  }
  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);
    }
    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");
  }
}





Enumeration of NetworkInterface

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
public class NetParms {
  public static void main(String[] args) throws SocketException {
    Enumeration<NetworkInterface> eni;
    eni = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface ni : Collections.list(eni)) {
      System.out.println("Name = " + ni.getName());
      System.out.println("Display Name = " + ni.getDisplayName());
      System.out.println("Loopback = " + ni.isLoopback());
      System.out.println("Up and running = " + ni.isUp());
      System.out.println("MTU = " + ni.getMTU());
      System.out.println("Supports multicast = " + ni.supportsMulticast());
      System.out.println("Sub-interfaces");
      Enumeration<NetworkInterface> eni2;
      eni2 = ni.getSubInterfaces();
      for (NetworkInterface ni2 : Collections.list(eni2))
        System.out.println("   " + ni2);
      System.out.println();
    }
  }
}





Get MAC address of a host: java.net.NetworkInterface.getHardwareAddress().

import java.net.InetAddress;
import java.net.NetworkInterface;
public class Main {
  public static void main(String[] args) throws Exception {
    InetAddress address = InetAddress.getLocalHost();
    NetworkInterface ni = NetworkInterface.getByInetAddress(address);
    byte[] mac = ni.getHardwareAddress();
    for (int i = 0; i < mac.length; i++) {
      System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
    }
  }
}





Interface Lister

import java.net.NetworkInterface;
import java.util.Enumeration;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
      NetworkInterface ni = (NetworkInterface) interfaces.nextElement();
      System.out.println(ni);
    }
  }
}





NetworkInterface

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
public class NetworkParameterDemo {
  public static void main(String[] args) throws Exception {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
      NetworkInterface ni = en.nextElement();
      printParameter(ni);
    }
  }
  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("");
    }
  }
}





Programmatic Access to Network Parameters

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
public class Main {
  public static void main(String args[]) throws SocketException {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
      displayInterfaceInformation(netint);
    }
  }
  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);
    }
    System.out.printf("%n");
  }
}





Report By Address

import java.net.InetAddress;
import java.net.NetworkInterface;
public class Main {
  static public void main(String args[]) throws Exception {
    InetAddress ia = InetAddress.getByName(args[0]);
    NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
    System.out.println(ni);
  }
}





Report By Name

import java.net.NetworkInterface;
public class Main {
  static public void main(String args[]) throws Exception {
    NetworkInterface ni = NetworkInterface.getByName(args[0]);
    System.out.println(ni);
  }
}