Java InstantCode. Developing Applications Using Java NIO

The NetCompConnect.java file implements the functions of the Network Information application. This file reads the hosts IP address and port number on which the end user wants to create the socket channel. The NetCompConnect.java file then retrieves information, such as echo time and current system date and time of the computer the end user selects. The NetCompConnect.java file passes this information to the CompInfo.java file, which sends it to the CompInfoDialog.java file. The CompInfoDialog.java file displays the computer information in a dialog box.

Listing 7-2 shows the contents of the NetCompConnect.java file:

Listing 7-2: The NetCompConnect.java File

/* Imports java.net package classes. */ import java.net.*; /* Imports java.io package classes. */ import java.io.*; /* Imports java.nio package classes.*/ import java.nio.*; import java.nio.channels.*; import java.nio.charset.*; import java.util.*; /* Class NetCompConnect - This class connects the application to the network using socket channel. This class also performs the ping and date/time operations. Fields: strCompAddr - Contains the IP address. mainApp - Creates the object of the NetCompFrame class. port - Contains the port number. charset - Contains the charset. decoder - Creates the decoder. buff - Creates the buffer. isa - Contains the object of the InetSocketAddress class. sc - Create the socket channel. Methods: connectComp() - This method creates the socket channel at the specified port number. run() - This method is invoked when the thread is started. */ public class NetCompConnect extends Thread { /* Declares the object of the String class. */ String strCompAddr = null; /* Declares the object of the NetCompFrame class. */ NetCompFrame mainApp = null; /* Declares the object of the Charset class. */ Charset charset; /* Declares the object of the CharsetDecoder class. */ CharsetDecoder decoder; /* Declares the object of the ByteBuffer class. */ ByteBuffer buff; /* Declares the object of the InetSocketAddress class. */ InetSocketAddress isa; /* Declares the object of the SocketChannel class. */ SocketChannel sc = null; int port = 0; /* Defines the default constructor. */ public NetCompConnect(String strCompAddr, NetCompFrame mainApp, int port) { this.strCompAddr = strCompAddr; this.mainApp = mainApp; this.port = port; } /* connectComp() - This method creates and initializes the socket at port 7 or 13 to retrieve the echo time or date/time of the specific computer. Parameter: strCompAddr - Stores the computer IP address. port - Stores the port number. Return Value: NA */ public int connectComp(String strCompAddr, int port) { String compInfo = ""; String temp = ""; String time = ""; String am_pm = ""; String date = ""; String date_time = ""; /* Gets the current time of the system in milliseconds. */ long startTime = System.currentTimeMillis(); try { /* Checks the port number, if port number is 13, this code is executed. */ if (port == 13) { /* Initializes the object of the Charset class. */ charset = Charset.forName("US-ASCII"); /* Initializes the object of the CharsetDecoder class. */ decoder = charset.newDecoder(); /* Initializes and allocates the size of the buffer. */ buff = ByteBuffer.allocateDirect(1024); /* Initializes the object of the InetSocketAddress class at port 13. */ isa = new InetSocketAddress(strCompAddr, port); try { /* Opens the socket channel. */ sc = SocketChannel.open(); /* Connects the socket channel to the specified IP address. */ sc.connect(isa); /* Clears the buffer. */ buff.clear(); /* Reads the data from the buffer. */ sc.read(buff); /* Flips the buffer. */ buff.flip(); /* Creates and initializes the object of CharBuffer and stores the decoded data from the buffer. */ CharBuffer cb = decoder.decode(buff); temp = new String(cb.array()); StringTokenizer st = new StringTokenizer(temp, " "); time = (String)st.nextToken(); am_pm = (String)st.nextToken(); date = (String)st.nextToken(); date_time = date + " " + time + " " + am_pm; compInfo = compInfo + date_time; } finally { if (sc != null) /* Close the socket channel. */ sc.close(); } /* Creates the object of CompInfo class and Adds this object to the compInfoVec vector of the NetCompFrame class. */ mainApp.compInfoVec.addElement(new CompInfo(strCompAddr, compInfo)); return 1; } /* Checks the port number, if port number is 7, this code is executed. */ else { /* Initializes the object of the InetSocketAddress class at port 7. */ isa = new InetSocketAddress(strCompAddr, port); try { /* Opens the socket channel. */ sc = SocketChannel.open(); /* Connects the socket channel to the specified IP address. */ sc.connect(isa); /* Gets the socket from the socket channel. */ Socket t = sc.socket(); /* Creates and initializes the object of DataInputStream class to retrieves the data from the socket. */ DataInputStream is = new DataInputStream(t.getInputStream()); /* Creates and initializes the object of PrintStream class to put the data to the socket. */ PrintStream ps = new PrintStream(t.getOutputStream()); ps.println("ping"); String str = is.readLine(); if (str.equals("ping")) { /* Gets the current system time. */ long endTime = System.currentTimeMillis(); /* Evaluates the echo time. */ compInfo = (endTime-startTime) + " ms"; /* Creates the object of CompInfo class and Adds this object to the compInfoVec vector of the NetCompFrame class. */ mainApp.compInfoVec.addElement(new CompInfo(strCompAddr, compInfo)); return 1; } else { return 0; } } finally { if (sc != null) /* Close the socket channel. */ sc.close(); } } } catch (Exception e) { return 0; } } /* run() - This method is executed when thread is started. Parameter: NA Return Value: NA */ public void run() { /* Calls the connectComp() method */ connectComp(this.strCompAddr, this.port); } }

 

Download this Listing .

In the above code, the constructor of the NetCompConnect class reads the host IP address, port number, and object of the NetCompFrame class. The methods defined in the above code are:

Категории