|
This Tech Tip reprinted with permission by java.sun.com
Java SE 6.0, code name Mustang, is fast approaching its Beta 2 early access release. One of the added features in Java SE 6 gives you access to more information than you could access before about network interfaces. It isn't uncommon to have systems running with multiple active network connections, such as wired, 802.11 a/b/g wireless, and bluetooth. Previous J2SE releases had limited support for access and discovery of information related to multiple connections. Java SE 6 expands this capability.
Introduced in J2SE 1.4, the NetworkInterface class offers access to some information about network interfaces. You could use the getNetworkInterfaces() method in NetworkInterface for information about the set of installed networks, or lookup a specific network with the getByName() or getByInetAddress() methods. You could then display information about the network interface, such as its name or its InetAddress. To see the kind of information you could access using NetworkInterface, run the following program, ListNets, in J2SE 5.0:
import java.io.*;
import java.net.*;
import java.util.*;
public class ListNets {
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");
}
}
|
If you run the ListNets program on a typical Microsoft Windows machine, your output should look something like the following -- the display names and addresses might be different based on your current hardware and setup:
Display name: MS TCP Loopback interface
Name: lo
InetAddress: /127.0.0.1
Display name: Intel(R) PRO/100 VE Network Connection -
Packet Scheduler Miniport
Name: eth0
Display name: RCA USB Cable Modem - Packet Scheduler Miniport
Name: eth1
InetAddress: /11.22.33.44
Linux machines would have similar output for names, but have different display names and possibly different addresses.
With methods such as isMCGlobal() and isMCSiteLocal(), the information you can get about each InetAddress is more related to multicasting and its address type then about the network interface itself. That network interface-related information is now available with the NetworkInterface class in Java SE 6.0.
Network interfaces can be hierarchically organized. The NetworkInterface class in Java SE 6.0 includes two methods, getParent() and getSubInterfaces(), that are pertinent to a network interface hierarchy. The getParent() method returns the parent NetworkInterface of an interface. In other words, if something is a subinterface, getParent() returns a non-null value. The getSubInterfaces() method returns all the subinterfaces of a network interface.
You can discover if a network interface is "up" (that is, running) with the isUp() method. There are also methods that tell you the type of the network interface: isLoopback() tells you if the network interface is a loopback interface, isPointToPoint() tells you if it's a point-to-point interface, and isVirtual() tells you if its a virtual interface.
Beyond basic status information, you can access other network parameters about a network interface such as its physical hardware address (as an array of bytes) and the Maximum Transmission Unit (MTU) (largest packet size).
The last item of information available for each NetworkInterface is a List of a new interface called InterfaceAddress. This gives you the InetAddress for this address, its broadcast address, and its subnet mask.
Here is an updated version of the ListNets program that uses the NetworkInterface enhancements:
import java.io.*;
import java.net.*;
import java.util.*;
public class ListNets {
private static final Console console = System.console();
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 {
console.printf("Display name: %s%n",
netint.getDisplayName());
console.printf("Name: %s%n", netint.getName());
Enumeration<InetAddress> inetAddresses =
netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(
inetAddresses)) {
console.printf("InetAddress: %s%n", inetAddress);
}
console.printf("Parent: %s%n", netint.getParent());
console.printf("Up? %s%n", netint.isUp());
console.printf("Loopback? %s%n", netint.isLoopback());
console.printf(
"PointToPoint? %s%n", netint.isPointToPoint());
console.printf(
"Supports multicast? %s%n", netint.isVirtual());
console.printf("Virtual? %s%n", netint.isVirtual());
console.printf("Hardware address: %s%n",
Arrays.toString(netint.getHardwareAddress()));
console.printf("MTU: %s%n", netint.getMTU());
List<InterfaceAddress> interfaceAddresses =
netint.getInterfaceAddresses();
for (InterfaceAddress addr : interfaceAddresses) {
console.printf(
"InterfaceAddress: %s%n", addr.getAddress());
}
console.printf("%n");
Enumeration<NetworkInterface> subInterfaces =
netint.getSubInterfaces();
for (NetworkInterface networkInterface : Collections.list(
subInterfaces)) {
console.printf("%nSubInterface%n");
displayInterfaceInformation(networkInterface);
}
console.printf("%n");
}
}
|
Run the updated ListNets in Java SE 6.0. Again, the output depends on your system configuration. Note that some information might not be accessible for security reasons.
> java ListNets
Display name: MS TCP Loopback interface
Name: lo
InetAddress: /127.0.0.1
Parent: null
Up? true
Loopback? true
PointToPoint? false
Supports multicast? false
Virtual? false
Hardware address: null
MTU: 1520
InterfaceAddress: /127.0.0.1
Broadcast Address: /127.255.255.255
Network Prefix Length: 8
Display name: Intel(R) PRO/100 VE Network Connection -
Packet Scheduler Miniport
Name: eth0
Parent: null
Up? false
Loopback? false
PointToPoint? false
Supports multicast? false
Virtual? false
Hardware address: [0, 1, 2, 3, 4, 5]
MTU: 1500
Display name: RCA USB Cable Modem - Packet Scheduler Miniport
Name: eth1
InetAddress: /11.22.33.44
Parent: null
Up? true
Loopback? false
PointToPoint? false
Supports multicast? false
Virtual? false
Hardware address: [0, 2, 3, 4, 5, 6]
MTU: 1500
InterfaceAddress: /11.22.33.44
Broadcast Address: /11.22.33.255
Network Prefix Length: 22
The output shows that the network connection eth1 is up, and connected to the Internet with IP address 11.22.33.44. It also shows that network connection eth0 is down. The loopback interface is up (and should always be up).
Compare the results of the program to what you get from something like the ipconfig command (with the /all option). You'll see very similar results.
For more information about network programming with the Java platform, see the Custom Networking trail in the The Java Tutorial.
Copyright (c) 2004-2005 Sun Microsystems, Inc.
All Rights Reserved.
Related Tips
|
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.