The New Input/Output (NIO) Application Programming Interface (API) supports the java.nio and java.nio.channels packages for handling advance I/O file system and sockets, multiplexing, and non-blocking data transfer between the client and the server. The java.nio package contains ByteBuffer classes that allow you to store data in bytes. The java.nio.channels package provides various classes, such as FileChannel, SelectionKey, Selector, ServerSocketChannel, and SocketChannel. You can use these classes to initialize a socket, connect the socket to the server, and transfer data using this socket.
This chapter explains how to develop a Chat application using the java.nio and java.nio.channels packages.
Using the Chat application, an end user can send private and broadcast messages. A private message is sent to an individual end user , while a broadcast message is sent to all the end users who are connected to the chat server.
The Chat application contains two folders, Server and Client.
The Server folder contains the following files:
ChatServer.java : Initializes all _ the chat server classes. This is the main class of the chat server.
UAServer_Socket.java : Creates, _ registers, and maps a socket to broadcast a message to all the end users connected in a chat session.
PRServer_Socket.java: Creates, registers, and maps a socket to send private messages to a specified end user.
Msgbroadcast.java : Broadcasts messages to all the end users connected in a chat session.
SocketCallback.java : Uses sockets to communicate with the chat client.
AppendUserList.java : Stores the names of all the end users connected in a chat session.
The Client folder contains the following files:
ChatLogin.java : Creates the Chat Login window for the chat application.
ChatClient.java : Creates the main window of the chat application. This window contains a text pane and a list box that display the messages and the user list.
CClient.java : Implements the methods that are declared or called in the chat application client. This file connects the chat client to the chat server.
Messager.java : Defines an abstract _ method to read and write the message to the text pane of the chat application client.
Figure 2-1 shows the architecture of the Chat application:
In the Chat application, there are two separate modules, chat server and chat client. In the chat server, the ChatServer.java file calls the UAServer_Socket.java file and PRServer_Socket.java file to create sockets to send the private and broadcast messages. The UAServer_Socket.java file calls the Msgbroadcast.java and AppendUserList.java files. Next, the Msgbroadcast.java file calls the SocketCallback.java file to broadcast the message to all the chat users. The AppendUserList.java file calls the SocketCallback.java file to add a new user to the user list. The PRServer_Socket.java file again calls the SocketCallback.java file to send the private message to a specific chat user.
In the chat client, the ChatLogin.java file calls the ChatClient.java file to open the chat client window. Next, ChatClient.java calls the CClient.java and Messager.java files. The CClient.java file establishes a connection between the client and the server to send and receive messages. The Messager.java file provides an abstract method, message(), for chat users.