Java by API/java.net/NetworkInterface
Содержание
- 1 NetworkInterface: getByInetAddress(InetAddress addr)
- 2 NetworkInterface: getDisplayName()
- 3 NetworkInterface: getHardwareAddress()
- 4 NetworkInterface: getHardwareAddress() throws SocketException
- 5 NetworkInterface: getInetAddresses()
- 6 NetworkInterface: getInterfaceAddresses()
- 7 NetworkInterface: getMTU()
- 8 NetworkInterface: getName()
- 9 NetworkInterface: isLoopback()
- 10 NetworkInterface: isPointToPoint()
- 11 NetworkInterface: isUp()
- 12 NetworkInterface: isVirtual()
- 13 NetworkInterface: supportsMulticast()
NetworkInterface: getByInetAddress(InetAddress addr)
<source lang="java">
import java.net.InetAddress; import java.net.NetworkInterface; public class ReportByAddress {
static public void main(String args[]) throws Exception { InetAddress ia = InetAddress.getByName("www.google.ca"); NetworkInterface ni = NetworkInterface.getByInetAddress(ia); System.out.println(ni); }
}
</source>
NetworkInterface: getDisplayName()
<source lang="java">
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"); }
}
</source>
NetworkInterface: getHardwareAddress()
<source lang="java">
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 Main {
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(""); } }
}
</source>
NetworkInterface: getHardwareAddress() throws SocketException
<source lang="java">
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) ? "-" : ""); } }
}
</source>
NetworkInterface: getInetAddresses()
<source lang="java">
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"); }
}
</source>
NetworkInterface: getInterfaceAddresses()
<source lang="java">
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 Main {
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(""); } }
}
</source>
NetworkInterface: getMTU()
<source lang="java">
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 Main {
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(""); } }
}
</source>
NetworkInterface: getName()
<source lang="java">
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"); }
}
</source>
NetworkInterface: isLoopback()
<source lang="java">
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 Main {
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(""); } }
}
</source>
NetworkInterface: isPointToPoint()
<source lang="java">
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 Main {
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(""); } }
}
</source>
NetworkInterface: isUp()
<source lang="java">
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 Main {
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(""); } }
}
</source>
NetworkInterface: isVirtual()
<source lang="java">
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 Main {
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(""); } }
}
</source>
NetworkInterface: supportsMulticast()
<source lang="java">
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 Main {
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(""); } }
}
</source>