Java Networking Classes and Interfaces

1. JAVA AND THE NET

Java has some distinct advantages over other programming languages as far as network applications are concerned. One of the important features of Java is that it supports 16-bit Unicode for International character sets, which is still difficult to implement in other languages. In addition, Java can excellently handle network security issues. These features together with other features such as platform independence and garbage collection allow us to develop efficient and elegant network applications without worrying about the system crashes, spread of viruses, or stealing of sensitive data.

This chapter demonstrates how to use Java network APIs to write basic network programs easily and quickly.

2. JAVA NETWORKING CLASSES AND INTERFACES

Java, like other programming languages, provides well-designed classes and interfaces (APIs) to most network features. Table 12.1 shows some of the commonly used network classes and interfaces. The network classes and interfaces are provided basically as three packages java.net, java.rmi, and javax.maii. The first two are parts of the JVM (Java Virtual Machine) and the last one can be downloaded from Oracle’s website. With this rich set of interfaces, writing network programs is quite simple. Programmers having experience in network programming in other languages can readily discover how easy it is to develop the same programs in Java. This chapter guides you through some of the basic networking classes and interfaces and provides examples. Advanced network programming concepts may be found in many chapters later.

 

3. GETTING NETWORK INTERFACES

Before developing actual network applications, let us first work with the network interfaces available on a host. A network interface is usually a Network Interface Card (NIC), that acts as a point of interconnection between a computer and other devices. Each network interface is represented by the Java class NetworkInterface.

Systems often run with multiple network interfaces such as wired Ethernet (802.3), WiFi (802.11 a/b/g), Bluetooth (802.15.4) etc. An application can use Networkinterface to specify which NIC to use for a particular network activity.

This class has no public constructor. However, it has many static methods such as

getNetworkInterfaces () , getByInetAddress () , getByName () , which can retrieve the interface details from the system. The first one returns the complete list of interfaces on the machine. The last two are used when the IP address or the name of the particular interface is already known. The following example program lists all the network interfaces and their sub-interfaces (if any).

//GetNetworkInterfaces.java

import java.net.*;

import java.util.*;

class GetNetworkInterfaces

{

public static void main(String args[]) throws Exception {

Enumeration<NetworkInterface> intfs =

NetworkInterface.getNetworkInterfaces();

while(intfs.hasMoreElements()) {

NetworkInterface intf = intfs.nextElement();

System.out.println(”\nInterface: ”+intf.getName());

System.out.println(”Display name: ”+intf.getDisplayName());

Enumeration<NetworkInterface> subIfs = intf.getSubInterfaces();

for (NetworkInterface subIf : Collections.list(subIfs)) {

System.out.printf(”\tSub Interface : ”+subIf.getName());

System.out.printf(”\tSub Interface Display

name :”+subIf.getDisplayName());

}

}

}

}

Network interfaces may have children interfaces, called sub-interfaces, which may be obtained using its getSubinterfaces() method. Similarly, if a network interface is a sub-interface, getParent() returns parent interface. Compile the program using the following command:

javac GetNetworkInterfaces.java

Now, run the program as follows:

java GetNetworkInterfaces

A sample output of this program is shown below:

Interface: lo

Display name: Software Loopback Interface 1

Interface: eth3

Display name: Realtek PCIe GBE Family Controller

Interface: net4

Display name: Atheros AR9285 802.11b/g/n WiFi Adapter

We may sometimes like to use a specific interface for data communication. We can query the system for the desired interface by its name as follows:

NetworkInterface intf = NetworkInterface.getByName(”net4”);

This interface can then be used subsequently for sending and receiving data (using socket, for example).

Note that network interfaces may not always be physical devices. It may be a logical device implemented by a software. The example includes the loopback interface, which is commonly used in test environments. It is not a physical device but a piece of software simulating a network interface.

1. Getting Interface Addresses

Each network interface is assigned one or more IP addresses. An IP address is represented by InetAddress class. It is sometimes necessary to know the IP address of an interface. This can be done using getInetAddresses() method that returns an Enumeration of InetAddress. The following program shows IP addresses of those interfaces that have at least one IPv4 address assigned.

import java.net.*;

import java.util.*;

class GetInterfaceAddresses

{

public static void main(String args[]) throws Exception

{

System.setProperty(”java.net.preferIPv4Stack”,”true”); Enumeration<NetworkInterface> intfs =

NetworkInterface.getNetworkInterfaces();
while(intfs.hasMoreElements())

{

NetworkInterface intf = intfs.nextElement();

Enumeration<InetAddress> addresses = intf.getInetAddresses();

if(addresses.hasMoreElements())

{

System.out.println(”\nName: ”+intf.getName());

System.out.println(”Display name: ”+intf.getDisplayName());

while(addresses.hasMoreElements())

{

InetAddress addr = addresses.nextElement();

System.out.println(”Address: ”+addr);

}

}

}

}

}

A sample output of this program is shown below:

Name: lo

Display name: Software Loopback Interface 1 Address: /127.0.0.1

Name: net4

Display name: Atheros AR9285 802.11b/g/n WiFi Adapter Address: /192.168.43.77

Name: eth4

Display name: VMware Virtual Ethernet Adapter for VMnet1 Address: /192.168.226.1

Name: eth5

Display name: VMware Virtual Ethernet Adapter for VMnet8 Address: /192.168.40.1

To get IPv6 addresses, comment the line

System.setProperty(”java.net.preferIPv4Stack”,”true”);

The following program prints the hostname and address of the local computer

import java.net.*; class LocalHost

{

public static void main (String args[]) throws UnknownHostException

{

InetAddress ia = InetAddress.getLocalHost();

System.out.println(”Name : ”+ia.getHostName());

System.out.println(”Address : ”+ia.getHostAddress());

}

}

Here is a sample output:

Name : UROY

Address : 192.168.226.1

2. Getting Interface Properties

The NetworkInterface has several other useful methods to gather information about a network interface. The following program (GetInterfaceParameters.java) displays information about network interfaces whose name is specified as a command line argument.

//GetInterfaceParameters.java import java.net.*;

import java.util.*;

class GetInterfaceParameters

{

public static void main(String args[]) throws Exception

{

NetworkInterface intf = NetworkInterface.getByName(args[0]);

System.out.println(”\nName : ”+intf.getName());

System.out.println(”Display name : ”+intf.getDisplayName());

System.out.println(”Up : ”+ intf.isUp());

System.out.println(”Loopback : ”+ intf.isLoopback());

System.out.println(”PointToPoint : ”+intf.isPointToPoint());

System.out.println(”Supports multicast :”+intf.supportsMulticast());

System.out.println(”Virtual : ”+intf.isVirtual());

byte[] mac1 = intf.getHardwareAddress();

if(mac1 != null)

{

System.out.print(”Hardware Address : ”); for (int k = 0; k < mac1.length; k++)

System.out.format(”%02X%s”, mac1[k], (k < mac1.length – 1) ? ”-” : ””);

System.out.println();

}

System.out.println(”MTU :”+intf.getMTU());

}

}

When the program was executed with the argument eth4, it generated the following output:

Name : net4

Display name : Atheros AR9285 802.11b/g/n WiFi Adapter

Up : true

Loopback : false

PointToPoint : false

Supports multicast :true

Virtual : false

Hardware Address : D0-DF-9A-01-55-E6 MTU :1500

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *