The
java.net.NetworkInterface class represents both types of interfaces.
NetworkInterface is useful for a multihomed system, which is a system with
multiple NICs. Using NetworkInterface, you can specify which
NIC to use for a particular network activity.
For example, assume you have a machine with two configured NICs, and you want to send data to a server. You create a socket like this:
Socket soc = new java.net.Socket(); soc.connect(new InetSocketAddress(address, port));
NetworkInterface nif = NetworkInterface.getByName("bge0");
Enumeration nifAddresses = nif.getInetAddresses();
Socket soc = new java.net.Socket();
soc.bind(nifAddresses.nextElement());
soc.connect(new InetSocketAddress(address, port));
You can also use NetworkInterface to identify the local interface
on which a multicast group is to be joined. For example:
NetworkInterface nif = NetworkInterface.getByName("bge0");
MulticastSocket() ms = new MulticastSocket();
ms.joinGroup(new InetSocketAddress(hostname, port) , nif);
NetworkInterface can be used with Java APIs in many other ways beyond the two uses described here.