Trail: Custom Networking
Lesson: Programmatic Access to Network Parameters
What Is a Network Interface?
Home Page > Custom Networking > Programmatic Access to Network Parameters
What Is a Network Interface?
A network interface is the point of interconnection between a computer and a private or public network. A network interface is generally a network interface card (NIC), but does not have to have a physical form. Instead, the network interface can be implemented in software. For example, the loopback interface (127.0.0.1 for IPv4 and ::1 for IPv6) is not a physical device but a piece of software simulating a network interface. The loopback interface is commonly used in test environments.

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));
To send the data, the system determines which interface will be used. However, if you have a preference or otherwise need to specify which NIC to use, you can query the system for the appropriate interfaces and find an address on the interface you want to use. When you create the socket and bind it to that address, the system will use the associated interface. For example:
  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.

Previous page: Programmatic Access to Network Parameters
Next page: Retrieving Network Interfaces