The New Input/Output (NIO) API provides the java.nio, java.nio.channels, and java.nio.charset packages for buffer management, socket handling, and data transfer. The java.nio package contains the ByteBuffer and CharBuffer classes that you can use to read and store the bytes and characters. To connect the application to a network, you can use the SocketChannel class that is available in the java.nio.channel package. The java.nio.charset package provides two classes, Charset and CharsetDecoder. You can use these classes to set the character sets and decode the bytes to characters .
This chapter explains how to develop a Network Information application using the java.nio, java.nio.channels, and java.nio.charset packages.
The Network Information application displays a list of computers connected in a network. Using this application, you can display the echo time or the current system date and time of a specific computer. For example, ABC Inc has corporate offices in three countries , X, Y, and Z. A network administrator in ABC Inc's office in X wants to know the time displayed in the HR Manager's computer, which is located at Y. The Network Information application gives the network administrator the ability to retrieve this information. The Network Information application uses the following files:
NetCompFrame.java: Creates a user interface for the Network Information application that helps display a list of network computers.
NetCompConnect.java: Implements the functions of the Network Information application. This file connects the application to the network using socket channels.
CompInfo.java: Provides information about the network computer.
CompInfoDialog.java: Creates a dialog box that displays the echo time and current system date and time of a computer.
Figure 7-1 shows the architecture of the Network Information application:
In the Network Information application, the NetCompFrame.java file calls the NetCompConnect.java and CompInfoDialog.java files. The NetCompConnect.java file connects the application to the network at the specified port number and calls the CompInfo.java file to retrieve the computer information. The CompInfoDialog.java file displays the computer information by calling the CompInfo.java file.